Android Notes-Active lifecycle &bundle reclaim temporary Data & Activity startup mode & Common Tips

Source: Internet
Author: User

The Android use task management activity is a collection of activities that are stored in the return stack (back stack);

The system always shows the top of the stack activity to the user;

Start new activity--new activity into the stack

Press the back key or call finish () to stack top active out

Four states of the active life cycle:

1. Operating status: At the top of the stack

2. Paused state: Not at the top of the stack, but still visible

3. Stop state: Not at the top of the stack, completely invisible, the system still saves its state and member variables, but unreliable

4. Destruction status: Removed from the stack, activity is reclaimed by the system

Seven callback methods for activity:

1. OnCreate (): Called when an activity is created for the first time, typically completing an initialization operation

2. OnStart (): Called when an activity becomes visible from invisible, typically loads a resource

3. Onresume (): Called when the activity is ready to interact with the user, at the top of the stack, running state

4. OnPause (): Called when the system starts or resumes another activity

5. OnStop (): Called when the activity is completely invisible, usually completes freeing memory and resource operations

6. OnDestroy (): Called before destruction, then to destroy state

7. Onrestart (): Called on reboot, changed from stop state to run state

Main differences between OnPause () and OnStop (): When starting a new activity for a dialog-box activity, execute OnPause (), do not execute onstop ();

Three lifetimes of activity:

1. Full lifetime: Period between onCreate () and OnStop ()

2. Visible lifetime: Period between OnStart () and OnStop ()

3. Foreground lifetime: Period between Onresume () and OnPause ()

Cases:

Mainactivity is first created when executed OnCreate (), OnStart (), Onresume ();

Click Button1 Start normalactiviy,mainactivity Execute OnPause (), OnStop ()

Click Button2 start dialogactivity,mainactivity only perform onpause ()

Because the dialogactivity is not completely occluded mainactivity,mainactivity just goes into a paused state instead of a stop state

Click the Back button to return to Mainactivity, only perform onresume ()

In Mainactivity Click the Back button to exit the program, execute OnPause (), OnStop (), OnDestroy ()

Save temporary data through bundles during activity recycling

When the activity enters a stopped state, it may be reclaimed by the system when the memory is low. Because the method onsaveinstancestate () is bound to be called before the activity is recycled, you can override this method to not guarantee the storage of temporary data:

@Override protected void onsaveinstancestate (Bundle outstate) {    super. Onsaveinstancestate (outstate);     = "Something you just typed";    Outstate.putstring ("Data_key", TempData);}

Modify the OnCreate method and add it after the Setconetentview () statement:

if NULL ) {    = savedinstancestate.getstring ("Data_key");    LOG.D (TAG, TempData);}

If the activity is saved by Onsaveinstancestate () before being reclaimed by the system, OnCreate () accepts the bundle parameter with the saved data, which can be fetched by the corresponding value method.

Run the program, rotate the screen to make the system automatically destroy the activity, Onsaveinstance () is called, the screen switches after the system automatically creates the activity call OnCreate ()

There are four types of activation modes : Standard, Singletop, Singletask and singleinstance;

The startup mode can be specified in the Android:launchmode attribute of the Androidmanifest.xml <activity>;

Standard : The default startup mode, the system does not consider whether the activity is already in the return stack, each start will create a new activity instance;

Singletop: If the top of the stack is already the activity, it is used directly, and no new activity instance is created. If not at the top of the stack, create a new activity instance;

Singletask: If the active instance already exists in the stack, it is used directly, and all activities above the activity are stacked. If not, create a new activity instance;

SingleInstance: A separate new return stack is started to manage the activity, and the application that accesses the activity shares an (original) return stack, thus sharing the activity instance;

Tip: Quickly determine which activity is currently in

Create the class baseactivity, overriding its OnCreate () method:

protected void onCreate (Bundle saveinstancestate) {    super. OnCreate (saveinstancestate);    LOG.D ("thirdactivity", "Task ID is" +GetTaskID ());    Requestwindowfeature (window.feature_no_title);    Setcontentview (r.layout.third_layout);}

To have all activities inherit from the parent class baseactivity, the instance class name can be printed each time an activity is called oncreate () to determine which activity is currently in.

Tip: Quick Exit Program

New class Activitycollector as the activity Manager:

 Public classActivitycollector { Public Staticlist<activity> activities =NewArraylist<activity>();  Public Static voidaddactivity (activity activity) {Activities.add (activity); }     Public Static voidremoveactivity (activity activity) {activities.remove (activity); }     Public Static voidFinishall () { for(Activity activity:activities) {if(!activity.isfinishing ())            {activity.finish (); }        }    }}

Modify the code in Baseactivity and add it in OnCreate ():

Activitycollector.addactivity (this);

In OnDestroy (), add the following:

Activitycollector.removeactivity (this);

In a button listener event that requires a key to exit the program, add:

Activitycollector.finishall ();

Tip: Pass the data initiation activity

Add a Method ACTIONStart () in the activity secondactivity that needs to receive the data passed by intent:

 Public Static void ACTIONStart (context context, string data1, String data2) {    new Intent (context, secondactivity.< C10>class);    Intent.putextra ("param1", data1);    Intent.putextra ("param2", data2);    Context.startactivity (intent);}

In which the components of the intent are completed so that all the data required for the secondactivity are reflected in the method parameters;

Call directly in the button listener event that needs to start secondactivity:

Secondactivity.actionstart (firstactivity.  this, "Data1", "data2");

Reference: The first line of code

Android Notes-Active lifecycle &bundle reclaim temporary Data & Activity startup mode & Common Tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.