Android-Jetpack

Android Jetpack一-lifecycle

2020-02-23  本文已影响0人  唐_夏影

用于对组件需要在特定声明周期进行某些操作的接口,本质上来说就是将Activity/Fragment生命周期需要进行的一些操作抽离出来

项目源码,demo

参考自:

https://www.jianshu.com/p/78532ac86db6

1)环境配置

添加依赖

    implementation "androidx.lifecycle:lifecycle-common-java8:2.2.0"

需要将编译选项设置为java8

android {
    compileSdkVersion 29
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "com.example.lifecycle"
        minSdkVersion 15
        targetSdkVersion 29
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }
    }
    //将编译选项设置为Java1.8
    compileOptions {
        targetCompatibility 1.8
        sourceCompatibility 1.8
    }
}

2)继承于父类接口

创建父类接口,继承于默认公共接口

open class IPresenter: DefaultLifecycleObserver

3)具体实现


class FirstPresenter : IPresenter() {
    private val TAG = " Lifecycle"

    override fun onCreate(owner: LifecycleOwner) {
        Log.d(TAG, "Observer:onCreate: ")
    }

    override fun onStart(owner: LifecycleOwner) {
        Log.d(TAG, "Observer:onStart: ")
    }

    override fun onResume(owner: LifecycleOwner) {
        Log.d(TAG, "Observer:onResume: ")
    }

    override fun onPause(owner: LifecycleOwner) {
        Log.d(TAG, "Observer:onResume: ")
    }

    override fun onStop(owner: LifecycleOwner) {
        Log.d(TAG, "Observer:onResume: ")
    }

    override fun onDestroy(owner: LifecycleOwner) {
        Log.d(TAG, "Observer:onDestroy: ")
    }
}

4)Activity中完成绑定

class MainActivity : AppCompatActivity() {

    private val TAG = "Lifecycle"

    private var mPresenter: IPresenter? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        mPresenter = FirstPresenter()
        lifecycle.addObserver(mPresenter as FirstPresenter)
        btn_01.setOnClickListener {
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }
        Log.d(TAG, "MainActivity:onCreate: ")
    }


    override fun onStart() {
        super.onStart()
        Log.d(TAG, "MainActivity:onStart: ")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "MainActivity:onResume: ")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "MainActivity:onPause: ")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "MainActivity:onStop: ")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "MainActivity:onDestroy: ")
    }
}

5)运行程序

运行我们的程序,查看日志打印

D/Lifecycle: MainActivity:onCreate: 
D/ Lifecycle: Observer[onCreate]
D/Lifecycle: MainActivity:onStart: 
D/ Lifecycle: Observer[onStart]
D/Lifecycle: MainActivity:onResume: 
D/ Lifecycle: Observer[onResume]

跳转到另外的Activity

D/ Lifecycle: Observer[onPause]
D/Lifecycle: MainActivity:onPause: 
D/ Lifecycle: Observer[onStop]
D/Lifecycle: MainActivity:onStop: 

6)在Fragment中使用

在Fragment中使用可以在构造方法初始化

class BlankFragment : Fragment() {

    private val TAG = "Lifecycle"

    init {
        lifecycle.addObserver(FirstPresenter())
    }

    override fun onAttach(context: Context) {
        super.onAttach(context)
        Log.d(TAG, "BlankFragment[onAttach]")
    }


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "BlankFragment[onCreate]")
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        Log.d(TAG, "BlankFragment[onViewCreated]")
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_blank, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        Log.d(TAG, "BlankFragment[onActivityCreated]")
    }

    override fun onStart() {
        super.onStart()
        Log.d(TAG, "BlankFragment[onStart]")
    }

    override fun onResume() {
        super.onResume()
        Log.d(TAG, "BlankFragment[onResume]")
    }

    override fun onPause() {
        super.onPause()
        Log.d(TAG, "BlankFragment[onPause]")
    }

    override fun onStop() {
        super.onStop()
        Log.d(TAG, "BlankFragment[onStop]")
    }

    override fun onDestroyView() {
        super.onDestroyView()
        Log.d(TAG, "BlankFragment[onDestroyView]")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.d(TAG, "BlankFragment[onDestroy]")
    }

    override fun onDetach() {
        super.onDetach()
        Log.d(TAG, "BlankFragment[onDetach]")
    }
}

运行程序

7)LifecycleObserver

也可以通过继承LifecycleObserver类,通过注解的方式来实现

class MyLifeCycleObserver : LifecycleObserver {

    private val TAG = "Lifecycle"

    @OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onActivityCreate() {
        Log.d(TAG, "onActivityCreate")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_START)
    fun onActivityStart() {
        Log.d(TAG, "onActivityStart")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    fun onActivityResume() {
        Log.d(TAG, "onActivityResume")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    fun onActivityPause() {
        Log.d(TAG, "onActivityPause")
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_STOP)
    fun onActivityStop() {
        Log.d(TAG, "onActivityStop")
    }
}

注册的方式是一样的

    lifecycle.addObserver(MyLifeCycleObserver())

8)判断当前周期

  if (lifecycle.currentState.isAtLeast(Lifecycle.State.CREATED)) {
                Log.d(TAG, "当前生命周期是否是在create")
  }

9)GenericLifecycleObserver

@Suppress("DEPRECATION")
class MyGenericLifecycleObserver : GenericLifecycleObserver {

    private val TAG = " MyGenericLifecycleObserver"

    @SuppressLint("LongLogTag")
    override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) {
        when (event) {
            //onCreate
            Lifecycle.Event.ON_CREATE -> {
                Log.d(TAG, "onCreate")
            }
            //onStart
            Lifecycle.Event.ON_START -> {
                Log.d(TAG, "onStart")
            }
            //onResume
            Lifecycle.Event.ON_RESUME -> {
                Log.d(TAG, "onResume")
            }
            //onPause
            Lifecycle.Event.ON_PAUSE -> {
                Log.d(TAG, "onPause")
            }
            //onStop
            Lifecycle.Event.ON_STOP -> {
                Log.d(TAG, "onStop")
            }
            //onDestroy
            Lifecycle.Event.ON_DESTROY -> {
                Log.d(TAG, "onDestroy")
            }
            else -> {

            }
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读