Analysis of activity lifecycle and start-up mode of important components in Android development

Source: Internet
Author: User

The

activity is an application component that provides a screen that users can use to interact in order to accomplish a task. All operations in the

activity are closely related to the user, a component that is responsible for interacting with the user, and can display the specified control through Setcontentview (View).

In an Android application, an activity is usually a separate screen that shows some controls that can also listen and handle user events to respond. Activity is communicated through intent.

Activity Lifecycle

Android activities from a back stack admin

Android activity has four states

1. Run status

When an activity is on top of the stack, This 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. It means that the activity is not completely covered, there is a layer of dialog boxes and the like.

3. Stop state

activity is not at the top of the stack and is not visible at all. This is a good understanding, it is the user can not see.

4. The Destroy status

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

activity has a total of seven callback methods, and is typically used for initialization when the active lifecycle

1.onCreate ()

activity creation is invoked. The

2.onStart ()

is called when it is not visible to be visible again.

3.onResume ()

is invoked when the activity reaches the top of the stack, which is called when the

4.onPause ()

starts or recovers another activity when the activity is ready and the user interacts.

5.onStop ()

is executed when the activity is completely invisible, and attention is completely invisible and will not start if the activity is initiated in the form of a dialog box.

6.onDestroy ()

Call

7.onRestart ()

Before the activity is destroyed the

when the activity is changed from a stop state to a running state the following diagram is an intuitive representation of the life cycle of an activity


 
You can use the following sample code to carefully explore what is printed through logcat you can easily figure it out

public class mainactivity extends activity {    public 
Static final string tag= "Mainactivity";      @Override     protected void oncreate (bundle 
savedinstancestate)  {        super.oncreate (savedInstanceState);
        LOG.D (TAG, "onCreate");
        LOG.D (Tag,this.tostring ());
        requestwindowfeature (Window.feature_no_title);
        setcontentview (R.layout.activity_main);         if (savedinstancestate!=null)          {            string temp=
Savedinstancestate.getstring ("Data_key");             LOG.D (tag,temp);         }         Button 
Startnormal= (Button) Findviewbyid (r.id.start_normal_activity);         button startdialog= (Button) Findviewbyid (R.id.start_dialog
_activity);         startnormal.setonclicklistener (New OnClickListener ()  {                           @Override              public void onclick (view v)  {                 intent intent=new intent (MainActivity.this,
Normalactivity.class);                &Nbsp;startactivity (Intent);             }       
  });         startdialog.setonclicklistener (New OnClickListener ()  {                           @Override              public void onclick (view v)  {                 intent intent=new intent (MainActivity.this,
Dialogactivity.class);                 startactivity (
Intent);             }       
  });     }      @Override     public void onstart () {     
   super.onstart ();
        LOG.D (TAG, "OnStart");    &NBSP}      @Override     public void  Onresume () {        super.onresume ();      
   LOG.D (TAG, "Onresume");    &NBSP}      @Override     public void  OnPause () {        super.onpause ();       
  LOG.D (TAG, "OnPause");    &NBSP}      @Override     public void onstop () {        super.onstop ();        
 LOG.D (TAG, "onStop");     }      @Override     public void ondestroy () {    
    super.ondestroy ();
        LOG.D (TAG, "OnDestroy");    &NBSP}      @Override     public void  Onrestart () {        super.onrestart ();      
   LOG.D (TAG, "Onrestart");     }


About startup mode

There are also four different activity launches.

To modify the start mode of an activity, you need to modify the Android:launchmode under the Activity tab in Androidmanifest.xml

<activity
android:name= ". Mainactivity "
android:launchmode=" singletop "
android:label=" @string/app_name ">
<intent-filt er>
<action android:name= "Android.intent.action.MAIN"/> <category
"Android:name=" Id.intent.category.LAUNCHER "/>
</intent-filter>
</activity>


 
1.standard

This is the active default boot mode, which creates a new instance of the activity on the stack every time you start

  It's like starting a firstactivity  on a firstactivity. If you want to return, you need to press two times to return to the desktop

2.singleTop

  This pattern solves the problem of the previous pattern well

If firstactivity This activity is already on top of the stack, then if you want to restart the firstactivity will not create a new instance of the

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

3.singleTask

Singletask perfectly resolves the creation of a duplicate activity instance. Each time the activity is started, it automatically finds that an instance of the activity exists in the stack, and if it is used directly, it does not exist to create a

4.singleInstance

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

Related Article

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.