Android development path (6) Activity Status transition

Source: Internet
Author: User

Reprinted please indicate from "LIU Da's csdn blog": http://blog.csdn.net/poechant


1. Application Lifecycle


The basic module in an Android Application is activity. Previously we tried how to implement switching between two activities. During switchover, the Migration logic of activity status is controlled by the developer during program design. It can be said that the lifecycle of the Android Application is controlled by controlling the lifecycle of the activity.


Like the previous blog, we created two activities named test1 and Test2 respectively, and switched them through intent. However, we need to override some methods and add logs to them to facilitate our observation of activity state conversion:


Public
Class test1extends activity {


...


Private
Staticfinal string
Tag = "activity-1 ";

Public
Void onstart (){

Super. onstart ();

Log. V (tag,
"Onstart ");

}

Public
Void onresume (){

Super. onresume ();

Log. V (tag,
"Onresume ");

}

Public
Void onpause (){

Super. onpause ();

Log. V (tag,
"Onpause ");

}

Public
Void onstop (){

Super. onstop ();

Log. V (tag,
"Onstop ");

}

Public
Void ondestroy (){

Super. ondestroy ();

Log. V (tag,
"Ondestory ");

}

Public
Void onrestart (){

Super. onrestart ();

Log. V (tag,
"Onrestart ");

}

}


Log. V (string tag, string MSG) is used. The first parameter tag can be used to distinguish between logs in test1 and test2. V indicates verbose, and D indicates debug, I indicates info, W indicates warn, and e Indicates error.


In the switchover between test1 and Test2, the corresponding log should be added, and an end button should be added to observe the kill process of the activity.


Public
Void oncreate (bundle savedinstancestate ){

Super. oncreate (savedinstancestate );

Setcontentview (R. layout. Main );

Button button1 = (button) findviewbyid (R. Id. button1 );

Button1.setonclicklistener (New button. onclicklistener (){

@ Override

Public
Void onclick (view paramview ){

Intent intent =
New intent ();

Intent. setclass (test. This, test2.class );

Startactivity (intent );

Test. This. Finish ();

}

});

Button button3 = (button) findviewbyid (R. Id. button3 );

Button3.setonclicklistener (New button. onclicklistener (){

@ Override

Public
Void onclick (view paramview ){

Test. This. Finish ();

}

});

}


The above is test1's oncreate, Test2 is similar. Other resource files and layout files are omitted here.


To avoid the inefficiency of "redo the wheel", you can refer to the blog titled "Eclipse debugging Android tool II-ddms" of this blogger for usage of ddms. We use ddms
Observed:



Load activity1 at startup:



Activity1 to activity2:



When activity2 ends:



It can be seen that the state transition of test1 starts from the creation of oncreate, And the onstart and onresume enter the running state. If it is switched to Test2, test1 will be onpause, while Test2 will go through the oncreate, onstart, and onresume processes. Next, Test2 is not displayed.
Test1. However, test1 still needs onstop and ondestory.


When there are many activities, their State control is very important. As for the complex situation where the state machine is required to express the state transition diagram so many complex activity relationships, I am afraid that the simple application Android app is still not used, and I cannot hold it.


Reprinted please indicate from "LIU Da's csdn blog": http://blog.csdn.net/poechant

-

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.