The lifecycle of activity in Android development and the detailed loading mode _android

Source: Internet
Author: User

This article gives you an introduction to the lifecycle of activity, and if you've learned about iOS partners, the activity lifecycleis very similar to the lifecycle of Viewcontroller in iOS. Life cycle is not difficult to understand. A person's life cycle is a sickness and death, the flower life cycle is the flower bloom thanks. The life cycle of activity in Android is the process of creating and disappearing activity. This blog will introduce the different stages of the activity lifecycle, and look at the life cycle of the activity in the form of an instance. Understanding the lifecycle of an activity is critical, because you can do different things in different segments only by understanding each stage of each lifecycle.

Next we will introduce the lifecycle of activity through an example, in which there will be a mainactivity, mainactivity is the first activity shown after app startup. There is a button on the mainactivity, click this button will jump to Secondactivity, click the Return key will be returned from the secondactivity to mainactivity , Clicking on the return key will exit the app. By switching the activity of these columns, we will look at the lifecycle of the activity by printing the log.

I. Code WRITING

In fact, this blog's code is very simple, is to rewrite the activity lifecycle of different stages of the method, and then in the method of log printing, so as to mark the life cycle phase. And the demo UI is also very simple, so the activity of the layout file will not do too much to repeat. Next, take a look at the key code in the two activity.

1. The code below is the key code in the mainactivity , overriding the different stages of the activity lifecycle, and then printing the log to mark which of the activity's methods. In the onCreate () method, get the button with an ID, click the event on the button binding, and jump to secondactivit y when you click on it. The specific code is as follows.

 @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
     Setcontentview (R.layout.activity_main);
     LOG.D ("Lifecycle", "mainactivity: I am OnCreate method, I will be first created in the activity is called");
     Button Jumpbutton = (button) Findviewbyid (R.id.jump_second_button);
         Jumpbutton.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {
         Jump to secondactivity Intent Intent = new Intent (mainactivity.this, Secondactivity.class);
       StartActivity (Intent);
   }
     });
     //ctrl + O Select methods in the parent class to override @Override protected void OnStart () {Super.onstart ();
   LOG.D ("Lifecycle", "mainactivity: I am the OnStart method, I will call" when the activity becomes visible by invisible);
     } @Override protected void Onresume () {super.onresume ();
   LOG.D ("Lifecycle", "mainactivity: I am the Onresume method, I invoke when the activity is in the running state");
     } @Override protected void OnPause () {super.onpause (); LOG.D ("Lifecycle", "MainacTivity: I am the OnPause method, I will call "when the activity pauses");
     } @Override protected void OnStop () {super.onstop ();
   LOG.D ("Lifecycle", "mainactivity: I am the OnStop method, I will call when the activity stops");
     } @Override protected void Onrestart () {Super.onrestart ();
   LOG.D ("Lifecycle", "mainactivity: I am Onrestart method, I will call" when activty from stop state to run state);
     } @Override protected void OnDestroy () {Super.ondestroy ();
   LOG.D ("Lifecycle", "mainactivity: I am OnDestroy method, I will call in Activty before destruction"); }

2. In secondactivity is also rewrite the above method, the only difference is that the above log in the mainactivity to Secondactivity, the code is similar to not paste.

Second, run the watch print log

Run our app, then watch the printed logs, and analyze the activity lifecycle through an instance. The specific steps are as follows:

1. Create and activate activity

The first time you open the app print log is shown in the following figure. Open the app first see mainactivity, through log we are not hard to see in Mainactiviy first appearance to invoke the following three methods, after the three methods below, Mainactivty will be created, and then become visible, finally in the running state.

(1). OnCreate () Method: This method is invoked the first time the activity is created, and in the previous demo we load the layout and or controls of the event and associate events in this method.

(2). OnStar () T method: then executes the OnStart () method, which is invoked when the activity becomes visible by the invisible State.

(3). Onresume Method: The activity becomes visible and then the Onresume method is invoked to become run state.

Switch between 2.Activity

To make it clearer, before clicking the button to jump to secondactivity, we can clear the log, and the bottom left has a trash bin logo, which is used to clear the previously printed log. The log printed below is the log that is printed when you click the Jump button. During this switching process, mianactivty stops running and becomes invisible, and Secondactivy is created and in a running state. The specific steps are as follows.

(1). OnPause () Method: When the Jump button is clicked, the mainactivity call OnPause () becomes a stop state, but it is still visible.

(2). Then secondactivity calls the OnCreate () method to create, calls the OnStart () method to display, and invokes the Onresume () method to run the procedure.

(3). OnStop () Method: When the secondactivity is in the running state, mainactivity is completely invisible, so the OnStop () method is invoked to enter the completely invisible stop state.

3. Return from another activity

Click the return key from the secondactivity to return to the mainactivity from the secondactivity. The log below is the log that is printed when you click the Return button.

(1) OnPause () method: After clicking the return button, Secondactivity will invoke the OnPause () method and enter the paused state.

(2) Onrestart () method: then mainactivity calls the Onrestart () method, which changes from a stop state to a running state. Mainactivity then calls the OnStart method from never visible to visible, and then calls the Onresume () method to finally enter the running state.

(3) when returned to Mainactivity and mainactivity is running, Secondactivity calls the OnStop method, and stops running is not visible. Because secondactivty do the stack operation, stop running, you will call the Ondestory method for destruction. This is done from the OnCreate method when you enter the secondactivity.

4.Activity Exit and Destroy

Then clicking the Back button in the mainactivity will exit the app, with log information printed when exiting the app. Mainactivty first calls the OnPause () method to a paused state, then calls the OnStop () method to stop running, and finally calls the OnDestroy () method for destruction.

Three. Life Cycle flow Chart

1. The above is to describe the lifecycle of the two activity transitions using a log printed in the language, followed by a flowchart to describe the process, as follows.

2. It is not difficult to analyze the phases of an activity's lifecycle through the above example, and it is easy to see which phases are executed, so it is easy to draw an activity's lifecycle, and the bottom flow chart is the lifecycle of an activity.

Four. Loading mode of activity

The activation mode of the activity is also simpler, and it is used during active switching. The starting mode of activity is divided into four kinds, standard, singletop, Singletask, singleinstance mode. Next, we will give you a detailed introduction to these several load modes.

The load mode of the activity can be configured in the configuration file Androidmanifest.xml, and the configuration item is android:launchmode as shown in the following illustration:

1.standard mode

In the activity stack, the activity is created regardless of whether the event has been added to the stack. The test method is to set the mainactivity Launchmode to standard, add a button to the Mainactivity, click the button to jump to the current activity, and see the log that is printed in the intent method. The way to click the button is as follows:

 Button Launchmodelbutton = (button) Findviewbyid (R.id.launch_model_button);
     Launchmodelbutton.setonclicklistener (New View.onclicklistener () {
       @Override public
       void OnClick (View v) {
         Intent Intent = new Intent (mainactivity.this, mainactivity.class);
         StartActivity (intent);
       }
     );

The stack of the standard load mode is as follows, regardless of whether or not an instance of the object is in the stack, it will be created.

2.singleTop mode

As long as the activity being created is not at the top of the stack, the activity is created into the stack. If the activity that will be created is at the top of the stack, the instance of the activity is not created. Test method, the above model directly into the singletop mode, mainactivty to their own jump will not create a new instance, will reuse the previous instance on the top of the stack. If mainactivty jumps to secondactivty, then secondactivity jumps to mainactivty again, then mainactivity will be created because the top of the stack is secondactivity. As shown below:

3.singleTask mode

Single task mode, this is also not difficult to understand, if from mainactivty jump to secondactivity, if you jump from secondactivty to Mainactivity, in a single task mode mainactivity has been in the stack, It will put its previous activity out of the stack, making it active at the top of the stack. The principle is shown in the following illustration:

4.singleInstance

Can be viewed as a single case pattern, which is special, and the activity that is set to SingleInstance will be placed in another stack, because it is so easy to share. The pattern in the top 3 is on the same stack. Below thirdactivity jumps to an activity in which the load mode is singleinstance.

  

Today's activity lifecycle is here first, and the relevant content is updated in the next blog post.

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.