The process of Activity from startup to destruction

Table of contents

1. Activity life cycle

2.Activity startup and destruction process

3.Activity stop process

4. The process of recreating Activity


1. Activity life cycle


2.Activity startup and destruction process

    After the system calls the onCreate() method, it will call onStart() immediately, then continue to call onResume() to enter the running state, and finally stop in the onResume state. After the startup is completed, the system will call onDestory() to end an Activity. The life cycle allows him to destroy the kill state.

  1. Create basic UI elements in onCreate;

  2. onPause and onStop: Clear the resources of the Activity to avoid waste;

  3. onDestory: Because the reference will be destroyed when the activity is destroyed, but the thread will not, so clear the opened thread.  


3.Activity stop process

  1. When the Activity at the top of the stack is partially invisible, there are actually two possibilities in the follow-up. From partially invisible to visible, that is, the recovery process, from partially invisible to completely invisible, that is, to stop the process. The system is not in the current Activity. onPause is called when it is visible.

4. The process of recreating Activity

  1. The system is in the stop state for a long time. At this time, when the system needs more memory or the memory is relatively tight, the system will recycle your Activity. In order to compensate you, it will save your Activity state to the Bundle through the onRestoreInstanceState() method. You can also add additional key-value pairs to save these states. When the Activity needs to be recreated, the saved Bundle object will be passed to the Activity's onRestoreInstance() method and onCreate() method. This is an important parameter of onCreate- -saveInstanceState;

  2. But saveInstanceState is not called every time the Activity leaves the foreground. If the user uses the finish method to end, it will not be called, and the Android system has implemented the cache state of the control by default.


  If there is any mistake, I hope all the big guys who are watching can point it out~

Guess you like

Origin blog.csdn.net/weixin_44715733/article/details/127500102