Android

Android架构组件的简单介绍及使用——Lifecycle,L

2017-09-24  本文已影响677人  甲虫007

Android Architecture Components介绍

安卓架构组件是一套帮助开发者开发健壮,可测试,可维护的组件。
包含Handling Lifecycles,LiveData,ViewMode,Room.四个大方面,每个方面都可以单独使用。本次文章主要写第一个组件的简单使用。后续慢慢写完后面的内容。

Handling Lifecycles包含的内容

1.Lifecycle
2.LifecycleObserver
3.LifecycleOwner

Lifecycle简单介绍

值得注意的是Android Architecture Components库还没正式发布。所以只有api 26.1.0及其之后的support library里面的Fragments和 Activities才实现了LifecycleOwner接口。如果是这之前的自定义的activities 和fragments需要实现LifecycleOwner 接口


Lifecycle events and states
LifecycleOwner简单介绍
LifecycleObserver简单介绍

简单示例

本例用的api版本小于26.1.0,所以MainActivity需要自己实现LifecycleOwner

import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.LifecycleRegistry;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity implements LifecycleOwner{
    //LifecycleRegistry 实现了Lifecycle 
    private LifecycleRegistry mLifecycleRegistry=new LifecycleRegistry(MainActivity.this);
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next);
        MyObserver myObserver = new MyObserver();
        mLifecycleRegistry.addObserver(myObserver);
    }

    @Override
    public Lifecycle getLifecycle() {
        return mLifecycleRegistry;
    }
}
import android.arch.lifecycle.Lifecycle;
import android.arch.lifecycle.LifecycleObserver;
import android.arch.lifecycle.LifecycleOwner;
import android.arch.lifecycle.OnLifecycleEvent;
import android.util.Log;

/**
 * Created by tony.zhang
 * Email: tony.zhang.Mr@foxmail.com
 */

public class MyObserver implements LifecycleObserver {
    private static final String TAG="MyObserver";

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    public void onCreated(){
        Log.d(TAG, "onCreated: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    public void onStart(){
        Log.d(TAG, "onStart: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void onResume(){
        Log.d(TAG, "onResume: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    public void onPause(){
        Log.d(TAG, "onPause: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    public void onStop(){
        Log.d(TAG, "onStop: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    public void customMethod(){
        Log.d(TAG, "customMethod: ");
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
    public void onAny(){//此方法可以有参数,但类型必须如两者之一(LifecycleOwner owner,Lifecycle.Event event)
        Log.d(TAG, "onAny: ");
    }
}
D/MyObserver: onCreated: 
D/MyObserver: onAny: 
D/MyObserver: onStart: 
D/MyObserver: onAny: 
D/MyObserver: onResume: 
D/MyObserver: customMethod: 
D/MyObserver: onAny: 
D/MyObserver: onPause: 
D/MyObserver: onAny: 
D/MyObserver: onStop: 
D/MyObserver: onAny: 
D/MyObserver: onAny: 
D/MyObserver: onPause: 
D/MyObserver: onAny: 
D/MyObserver: onStop: 
D/MyObserver: onAny: 
D/MyObserver: onStart: 
D/MyObserver: onAny: 
D/MyObserver: onResume: 
D/MyObserver: customMethod: 
D/MyObserver: onAny: 

最后注意

allprojects {
    repositories {
        jcenter()
        maven { url 'https://maven.google.com' }
    }
}
   compile 'android.arch.lifecycle:extensions:1.0.0-beta1'
   compile 'android.arch.lifecycle:runtime:1.0.0'
   annotationProcessor  'android.arch.lifecycle:compiler:1.0.0-beta1'

附录传送门

官方文档: https://developer.android.com/topic/libraries/architecture/lifecycle.html#lc-bp
demo :https://github.com/Totomiz/AndroidArchitectureComponets

上一篇 下一篇

猜你喜欢

热点阅读