Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Activity Lifecycle call in Android

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail the example analysis of Activity lifecycle calls in Android. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Status

The activity is stored in a collection called the return stack, which appears at the top of the stack when an Activity is reopened. When you want to destroy the activity, call finish () or back, and the activity at the top of the stack goes off the stack.

Running status: when an activity is at the top of the stack

Paused state: not at the top of the stack, but still visible. (loss of focus, non-full-screen or transparent activity placed on top of stack)

Stop state: it is not at the top of the stack and is not visible, but some data about variables is still reserved for it. (overwritten)

Destroy status: remove from the stack (killed status)

Startup mode

Standard mode: the default startup mode for Activity. There are no restrictions on the startup and switching of Activity. Whenever we start an activity in standard mode through startActivity or startActivityForResult (), the system will create an instance and place it on top of the stack. [when redirecting through intent, a new activity instance is created, which is created repeatedly]

SingleTop mode: when set to this mode, there cannot be two instances of Activity at the top of the task stack. However, there can be multiple instances throughout the stack. Prevent repeated creation or startup of the same Activity [activity redirected through intent at the top of the stack and directly reused; if not at the top of the stack, re-create]

SingleTask mode: only one instance of this activity can exist in the entire task stack. When starting repeatedly, the system destroys all other activity before the instance and moves the activity to the top of the stack. [app creates a task stack when it starts, and all the activity created in the application will be put into this stack. As long as the activity instance has been created, the existing instance will be reused directly when it is created again.]

SingleInstance mode: all Activity in this mode will be stored independently under a task stack and cannot be created or started repeatedly. When you start an Activity in singleInstance mode, the system creates a new foreground task stack for the application and places the Activity in the stack. [the instance is unique in the whole Android system, and you can let other people's programs reuse this Activity directly]

At other times, when you want to start other non-singleInstance mode Activity, the system creates another foreground task stack and places the non-singleInstance mode Activity on the foreground task stack in startup order. Move the task stack of the Activity in singleInstance mode to the background. Starting any Activity after that is a switch between the foreground and background task stack.

When starting the same Activity in singleInstance mode repeatedly, the system switches the task stack where the Activity is located to the foreground, and starts other types of Activity without switching the task stack and adding a new instance.

Lifecycle call when operating on APP

Click Activity and press the Home key to return to the home screen: onPause (), onStop ()

Home screen, click App again to return to Activity:onRestart (), onStart (), onResume ()

Open B Activity:An onPause (), B onCreate (), B onStart (), B on Resume (), An onStop () on An Activity

Click the back key: onPause (), onStop (), onDestroy ()

Activity exception life cycle

Abnormal life cycle: the Activity is reclaimed by the system or the Configuration of the current device changes (horizontal and vertical screen), causing the Activity to be destroyed and rebuilt

Changes in Configuration cause Activity to be killed and rebuilt

OnSave [Restore] InstanceState (): the system automatically calls, the configuration changes, and the Activity is destroyed abnormally to store data (before onStop). Call onRestore, passing the Bundle object saved through onSave as parameters to onRestore and onCreate. If destroyed normally, these two methods will not be called.

The difference between onRestore and onCreate: both can perform data recovery. However, once onRestore is called back by the system, its Bundle must not be empty and no additional judgment is needed. However, the onCreate will be empty, and when the Activity is started normally, the Bundle will be empty, which requires additional judgment.

To prevent Activity from rebuilding after system configuration changes: assign the configChange attribute [orientation, screenSize] to Activity. Orientation corresponds to API

< 13 , screenSize 对应API >

13 . After setting, when the horizontal and vertical screen is switched, it will not be rebuilt, but will be called back to onConfigurationChanged.

Insufficient memory causes low-priority Activity to be killed and out of memory, so the process that kills the target Activity according to a certain priority reclaims memory. OnSave is called to store the data, and onRestore is called to recover the data in subsequent restores.

This is the end of the article on "sample Analysis of Activity Lifecycle calls in Android". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report