Life Cycle Management of Android Development Series

Android's life cycle management is roughly divided into: the life cycle
from birth to death, the corresponding methods are onCreate and onDestroy; the
entry and exit stop state, the corresponding methods are onStart and onStop; the
entry and exit pause state, the corresponding methods are onResume and onPause.

Now take the stopwatch APP scenario as an example to summarize the applicable scenarios for the next three states:
START starts timing, STOP stops timing, and RESET starts timing from 0.
Insert picture description here

The life cycle from life to death is the largest life cycle management, that is, the state of an activity from being created to dying. At this time, the corresponding scenario is: if onDestroy and onCreate are called in turn when the Android screen is flipped, then there is no save before the flip When you want to save the state before the rollover, you can call the onSaveInstanceState method. When it is enabled again, the count value and the boolean value of whether to reset or not are retrieved.
The relevant code is:

protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if(savedInstanceState != null){
    
      //之前的状态
            seconds = savedInstanceState.getInt("seconds");
            running = savedInstanceState.getBoolean("running");
            wasRunning = savedInstanceState.getBoolean("wasRunning");
        }
        runTimer();  //定时器任务,定时取出Seconds值
    }

public void onSaveInstanceState(Bundle savedInstanceState){
    
    
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putInt("seconds",seconds);
        savedInstanceState.putBoolean("running",running);
        savedInstanceState.putBoolean("wasRunning",wasRunning);
    }

The use scenario of entering and exiting the pause state is when the APP enters the background after pressing the HOME key, the APP pauses timing; when returning to the foreground, the timing continues, and the previous state value also needs to be saved.
The relevant code is:

protected void onStart() {
    
    
        super.onStart();
        if(wasRunning){
    
    
            running = true;
        }
    }

protected void onStop() {
    
    
        super.onStop();
        wasRunning = running;
        running = false;
    }

The usage scenario of entering and exiting the pause state is when the mobile phone can be used in split screen and the APP is still visible after losing focus. At this time, the APP needs to be paused, and the timing will continue when the focus is regained. This state is the minimum state. The relevant code is:

protected void onResume() {
    
    
        super.onResume();
        if(wasRunning){
    
    
            running = true;
        }
    }

protected void onPause() {
    
    
        super.onPause();
        wasRunning = running;
        running = false;
    }

Summary:
Insert picture description here
The life cycle from birth to death is a large cycle, which represents the cycle from birth to death; the in and
out stop state is a middle cycle, which represents the cycle of visibility and disappearance; the in and
out pause state is a small cycle, which represents the cycle of gaining and losing focus.

Reference materials:
https://www.icourse163.org/learn/BFU-1205989803?tid=1450759471&from=study#/learn/content MOOC course on mobile development technology

Guess you like

Origin blog.csdn.net/langxiaolin/article/details/113877760