Introduces the Jetpack:

  • These are some of the questions Google raised in October ’18 and’ 19 to improve the experience android programmers have when developing applications.
  • Like iOS, in their programming world, their language is unified, their architecture is unified, and their workflow is unified

Lifecycle:

What is Lifecycle good for?

  • Is a great tool for activity and fragment lifecycle monitoring
  • Keeping the code observing the life cycle out of the activity avoids excessive code volume and coupling

What is Lifecycle?

  • Copy off the source code, remove the comments, and all that remains is Lifecycle’s abstract class, which turns out to be an observer

  • First, understand the nature of things

How does Lifecycle work?

  • Write code and experience it
    • Create a new project and add the dependencies on the right side of the first image. For kotlin, add kapt

  • New Observer
    • In fact, we can directly implement its superclass, its superclass does not implement any method, and this method is not useful to us at the moment

  • What do we need to do to observe the activity lifecycle?
    • We need to write a method with an arbitrary method name

  • Binding activity
    • The activity implements the Lifecycle interface. There is a method in the interface that retrieves Lifecycle, calls it, retrieves Lifecycle, and binds the observer

Go through the source code

  • How does it work in an activity?
    • ComponentActivity implements the LifecycleOwner interface,
    • Provide a getLifecycle method
      • This method returns LifecycleRegistry

  • LifecycleRegistry is a Lifecycle

  • It also has its add, remove methods
  • How does he sense the cycle of activity? And then call setState, setCurrentState?
    • Look for it in his ComponentActivity onCreat
    • They were flat in the bag. It had to be related

  • The ReportFragment is a Fragment

  • It injects itself into the activity, and the fragment in the activity of course synchronizes its lifecycle with the activity

  • And then dispatch in what cycle

  • Dispatch delivers the message to Lifecycle

  • So Lifecycle actually goes through
    • Create a Fragment
    • Inject fragments into the Activity to be bound
    • Send lifecycle changes to listeners through fragments