Life cycle and startup mode for Android development activity

Source: Internet
Author: User

Activity life cycle

Android activity is managed by a return stack

There are four status of Android activity

1. Operational status

When an activity is at the top of the stack, the activity is in the running state, which is the interface that interacts with the user.

2. Pause Status

When the activity is not on top of the stack, it is still visible. This means that the activity is not completely covered, with a layer of dialog boxes.

3. Stop state

The activity is not at the top of the stack and is completely invisible. This is a good understanding, it is the user can not see.

4. Destruction Status

The activity was removed from the stack, which was closed by the user.

Activity has a total of seven callback methods covering the entire life cycle of activities

1.onCreate ()

Called when an activity is created, which is typically used to initialize.

2.onStart ()

Called when it is re-visible by invisible.

3.onResume ()

Called when the activity arrives at the top of the stack, which is the event preparation and user interaction.

4.onPause ()

Called when the other activity is started or resumed.

5.onStop ()

It is performed when the activity is completely invisible and is not visible at all, and is not started if the activity is started in a dialog box.

6.onDestroy ()

Called before the activity is destroyed

7.onRestart ()

Called when an activity is changed from a stopped state to a running state

A very intuitive representation of the life cycle of the activity

You can use the following example code to explore the contents of the Logcat printing, you can easily figure out

1  Public classMainactivityextendsActivity {2 3      Public Static FinalString tag= "Mainactivity";4 @Override5     protected voidonCreate (Bundle savedinstancestate) {6         Super. OnCreate (savedinstancestate);7LOG.D (TAG, "OnCreate");8LOG.D (TAG, This. toString ());9 requestwindowfeature (window.feature_no_title);Ten Setcontentview (r.layout.activity_main); One         if(savedinstancestate!=NULL) A         { -String temp=savedinstancestate.getstring ("Data_key"); - log.d (tag,temp); the         } -Button startnormal=(Button) Findviewbyid (r.id.start_normal_activity); -Button startdialog=(Button) Findviewbyid (r.id.start_dialog_activity); -  +Startnormal.setonclicklistener (NewOnclicklistener () { -              + @Override A              Public voidOnClick (View v) { atIntent intent=NewIntent (mainactivity. This, Normalactivity.class); - startactivity (intent); -             } -         }); -Startdialog.setonclicklistener (NewOnclicklistener () { -              in @Override -              Public voidOnClick (View v) { toIntent intent=NewIntent (mainactivity. This, Dialogactivity.class); + startactivity (intent); -             } the         }); *  $     }Panax Notoginseng @Override -      Public voidOnStart () { the         Super. OnStart (); +LOG.D (TAG, "OnStart"); A     } the @Override +      Public voidOnresume () { -         Super. Onresume (); $LOG.D (TAG, "Onresume"); $     } - @Override -      Public voidOnPause () { the         Super. OnPause (); -LOG.D (TAG, "OnPause");Wuyi     } the @Override -      Public voidOnStop () { Wu         Super. OnStop (); -LOG.D (TAG, "OnStop"); About     } $ @Override -      Public voidOnDestroy () { -         Super. OnDestroy (); -LOG.D (TAG, "OnDestroy"); A     } + @Override the      Public voidOnrestart () { -         Super. Onrestart (); $LOG.D (TAG, "Onrestart"); the}

About startup mode

There are also four types of activity launches

To modify the activity's startup mode, you need to modify the Android:launchmode under the Activity tab in Androidmanifest.xml

1        <Activity2             Android:name=". Mainactivity "3 Android:launchmode= "Singletop"4 Android:label= "@string/app_name" >5             <Intent-filter>6                 <ActionAndroid:name= "Android.intent.action.MAIN" />7                 <categoryAndroid:name= "Android.intent.category.LAUNCHER" />8             </Intent-filter>9         </Activity>

1.standard

This is the default startup mode for the activity, a startup mode that creates a new instance of the activity in the stack each time it is started

It's like starting a firstactivity on firstactivity. If you want to go back, you're going to have to press two times to return to the desktop.

2.singleTop

This model solves the problem of the previous pattern well.

If Firstactivity is already at the top of the stack, then if you're going to start firstactivity again, you won't be creating a new instance.

However, this method is limited to firstactivity at the top of the stack, if the firstactivity is not on the top of the stack, that is, not the user can see the interface, then will create a new instance.

3.singleTask

Singletask perfectly solves the case of creating a repeating activity instance. Each time the activity is started, it automatically finds out if there is an instance of the activity in the stack, and if it exists directly, it does not exist to create

4.singleInstance

This is very special, when it starts the activity, it creates a new stack to hold the newly launched activity. This pattern solves the problem of invoking activities between different applications.

 

Life cycle and startup mode for Android development activity

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.