美文网首页
AndroidX-Jetpack-Lifecycle

AndroidX-Jetpack-Lifecycle

作者: 飞奔吧牛牛 | 来源:发表于2020-11-19 16:39 被阅读0次

    当前版本:
    compileSdkVersion 29
    implementation 'androidx.appcompat:appcompat:1.2.0'

    第一步:创建Activity继承AppCompatActivity类。

    public class LifeCycleActivity extends AppCompatActivity {
     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_life_cycle);
    
        }
    }
    

    第二步:定义一个类MyLifeCycleObserver,实现LifecycleObserver接口。

    在MyLifeCycleObserver中定义方法,加上@OnLifecycleEvent注解
    import android.util.Log;
    
    import androidx.lifecycle.Lifecycle;
    import androidx.lifecycle.LifecycleObserver;
    import androidx.lifecycle.OnLifecycleEvent;
    public class MyLifeCycleObserver implements LifecycleObserver {
        private static final String TAG = MyLifeCycleObserver.class.getSimpleName();
    
        @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
        public void onCreate() {
            Log.e(TAG, "onCreate");
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_START)
        public void onStart() {
            Log.e(TAG, "onStart");
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
        public void onResume() {
            Log.e(TAG, "onResume");
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
        public void onPause() {
            Log.e(TAG, "onPause");
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
        public void onStop() {
            Log.e(TAG, "onStop");
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY)
        public void onDestory() {
            Log.e(TAG, "onDestory");
        }
    
        @OnLifecycleEvent(Lifecycle.Event.ON_ANY)
        public void onAny() {
            Log.e(TAG, "onAny");
        }
    
    }
    

    第三步:在Activity中,getLifecycle().addObserver(new MyLifeCycleObserver());

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.e(TAG, "onCreate    enable:"+enable());
            setContentView(R.layout.activity_life_cycle);
            getLifecycle().addObserver(new MyLifeCycleObserver());
        }
    

    MyLifeCycleObserver 由创建到销毁,其生命周期方法的会被MyLifeCycleObserver观察到,并依次执行其生命周期相关联的方法。
    运行结果如下

    2020-11-19 10:11:34.793 20430-20430/com.example.jetpack E/LifeCycleActivity: onCreate
    2020-11-19 10:11:34.815 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onCreate
    2020-11-19 10:11:34.815 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onAny
    
    2020-11-19 10:11:34.818 20430-20430/com.example.jetpack E/LifeCycleActivity: onStart
    2020-11-19 10:11:34.818 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onStart
    2020-11-19 10:11:34.818 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onAny
    
    2020-11-19 10:11:34.818 20430-20430/com.example.jetpack E/LifeCycleActivity: onResume
    2020-11-19 10:11:34.818 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onResume
    2020-11-19 10:11:34.819 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onAny
    
    2020-11-19 10:11:38.678 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onPause
    2020-11-19 10:11:38.678 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onAny
    2020-11-19 10:11:38.678 20430-20430/com.example.jetpack E/LifeCycleActivity: onPause
    
    2020-11-19 10:11:39.278 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onStop
    2020-11-19 10:11:39.278 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onAny
    2020-11-19 10:11:39.278 20430-20430/com.example.jetpack E/LifeCycleActivity: onStop
    
    2020-11-19 10:11:39.279 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onDestory
    2020-11-19 10:11:39.279 20430-20430/com.example.jetpack E/MyLifeCycleObserver: onAny
    2020-11-19 10:11:39.280 20430-20430/com.example.jetpack E/LifeCycleActivity: onDestroy
    

    需要注意的是:

    当activity生命周期方法触发时,会同时回调到这里
    需要注意的是:
    1.onCreate、onStart和onResume三个方法,会在对应的Activity方法执行完之后执行,
    而onPause、onStop和onDestory 三个方法,会在对应的Activity方法之前执行。
    2.onAny方法会在LifecycleObserver的每个生命周期方法执行之后调用。

    相关文章

      网友评论

          本文标题:AndroidX-Jetpack-Lifecycle

          本文链接:https://www.haomeiwen.com/subject/gkmkiktx.html