Android learning (Activity life cycle and startup mode)

Table of contents

  1. Activity life cycle

  2. Activity startup mode


Activity life cycle:

Life cycle under normal circumstances:

  • onCreate(): The system will call it when the Activity is instantiated for the first time. This method will only be called once in the entire life cycle. Usually used for initialization settings, setting the layout file to be used for Activity, binding listeners for buttons and other static setting operations.
  • onStart(): The system calls it when the Activity is visible, has not received user focus, and cannot be interacted with.
  • onRestart(): The system calls it when the Activity has been stopped and then restarted.
  • onResume(): The system calls it when the Activity is visible and has user focus and can interact.
  • onPause(): used to store persistent data. At this point it is visible but not interactive, and the system will stop animations and other CPU-consuming things. From the above description, we already know that some of your data should be saved here, because at this time, the priority of your program is reduced and it may be taken back by the system.
  • onStop(): Called by the system when the Activity is completely covered and invisible by a new Activity.
  • onDestroy(): System call when the Activity (user calls finish() or the system is destroyed due to insufficient memory) is destroyed by the system (only called once in the entire life cycle) to release the resources created in the onCreate() method, such as ending Threads etc.

 When we open a new Activity from an old Activity, the life cycle of the old Activity and the new Activity:

  1. The old Activity calls onPause()
  2. The new Activity calls onCreate()
  3. The new Activity calls onStart()
  4. The new Activity calls onResume()
  5. The old Activity calls onStop()

Activity life cycle under abnormal circumstances: 

  First learn the onSaveInstanceState and onRestoreInstanceState methods:

        onSaveInstanceState is used to save the UI state. You can use it to save what you want to save. Before the Activity is killed, it is usually triggered before onStop or onPause. OnRestoreInstanceState triggers the recovery state before onResume. As for overriding this method Whether the onCreate method will be called later.

1. The Activity is killed, onCreate will be called, and onRestoreInstanceState restores the last saved information before onResume.

2. The Activity is not killed and onCreate will not be called, but onRestoreInstanceState will still be called to restore the last saved information before onResume.

       onSaveInstanceState and onRestoreInstanceState are a pair of brothers, one is responsible for storage and the other is responsible for retrieval. They are not necessarily called in pairs.

       Activity's onSaveInstanceState() and onRestoreInstanceState() are not life cycle methods. They are different from onCreate(), onPause() and other life cycle methods, and they are not necessarily triggered. When the application encounters an unexpected situation (such as insufficient memory, the user directly presses the Home button) and the system destroys an Activity, onSaveInstanceState() will be called. But when the user actively destroys an Activity, such as pressing the return key in the application, onSaveInstanceState() will not be called. Because in this case, the user's behavior determines that the state of the Activity does not need to be saved. Usually onSaveInstanceState() is only suitable for saving some temporary states, while onPause() is suitable for persistent saving of data.
           Call to save the state of each instance before the activity is killed to ensure that the state can be restored in onCreate(Bundle) or onRestoreInstanceState(Bundle) (the incoming Bundle parameter is encapsulated by onSaveInstanceState). This method is called before an activity is killed, so that when the activity comes back at some time in the future, it can restore its previous state. 

onSaveInstanceState (called when Activity is destroyed unexpectedly)

 Situation 1: Resource-related system configuration changes cause the Activity to be killed and recreated

  • When an unexpected situation occurs in Activity
  • Call onSaveInstanceState() to save the UI state
  • Call onDestory() to destroy the Activity and release resources
  • When the Activity is recreated
  • Call onCreate() to create
  • Call onRestoreInstanceState() to restore the UI state (called before onStart)

 Next there is a question:

If a certain configuration changes, can we not recreate the Activity?

sure.

Here is a configChanges attribute mentioned

For example, if we don't want the Activity to be rebuilt when it rotates, we can add an orientation value to the configChanges attribute, as shown below:

android:configChanges="orientation"

If you want multiple values, you can use "|" to connect them

Other values ​​for the configChanges attribute:

   VALUE DESCRIPTION 
mcc The country code of the International Mobile Subscriber Identity has changed. The sim has been detected. Update the mcc. MCC is the country code of the mobile subscriber.
mnc The mobile network number of the International Mobile Subscriber Identity has changed. The sim has been detected. Update the mnc. MNC is the mobile network number, consisting of up to two digits. It is used to identify the mobile communication network to which the mobile user belongs.
locale When the user's region changes, usually when the user switches languages, the switched language will be displayed.
touchscreen The touch screen is changed ------ usually doesn't happen
keyboard The keyboard has changed - for example the user uses an external keyboard
keyboardHidden Keyboard usability has changed
navigation Navigation changes -----Normally that doesn't happen either
screenLayout The display of the screen changes------different displays are activated
fontScale The font scale has changed----a different global font has been selected
uiMode User patterns have changed
orientation The screen orientation has changed---switching between horizontal and vertical screens
screenSize Screen size changed
smallestScreenSize The physical size of the screen has changed, e.g. connected to an external screen

 Situation 2: Insufficient resource memory causes low-priority Activity to be killed

  • Foreground Activity----------Activity that is interacting with the user, has the highest priority
  • Visible but not foreground Activity) ---------Visible but unable to interact with the user (in onStart state
  • Background Activity---------Activity that has been suspended, with the lowest priority (in onPause and onStop states)

Subsequently, onSaveInstanceState and onRestoreInstanceState will be called to store and restore data.

 

Activity startup mode

standard (standard mode):
        The system's default startup mode, stack structure, first in, last out, open an activity and push it into the stack, then open the next activity, and then push it into the stack. The activities we usually create directly are all activities in this mode.

The characteristics of this mode of Activity are: as long as you create an Activity instance, once the Activity is activated, the newly created instance will be added to the task stack, and the instance will be destroyed in the task stack when exiting the Activity.

singleTop (top-of-stack mode):
        If an Activity activates itself, that is, the top of the task stack is the Activity, there is no need to create it. In other cases, an Activity instance must be created;

        for example:

        The current stack is ABCD, A is the bottom of the stack, and D is the top of the stack. At this time, we start a D (D is singleTop mode), and the stack situation is ABCD (if D is standard mode, the stack becomes ABCDD).

singleTask (in-stack reuse mode):
        If the Activity to be activated exists as an instance in the task stack, there is no need to create it. You only need to put this Activity on the top of the stack and destroy all Activity instances above this Activity;

singleInstance (single instance mode):
        If we set an activity to this singleStance startup mode, then when the activity is activated, it will be placed on a separate stack. The next time it is used, this stack will be used directly, such as a phone call application. Activity started in singleStance mode

 

Guess you like

Origin blog.csdn.net/qq_62277763/article/details/127936767