Preface: There are many people in life accompany themselves through a journey of a lifetime, only some people are only a certain stage, some people are accompanied by their own very long. Like elementary school, middle school, High school, university, those who thought will have long ago, when experienced in the end of the world busy life, or wishful. Or frustrated, and gradually the pictures are just looking back. Tianya each life, can be together in the effort to cherish it, not together on the heartfelt blessing, we all need a posture to live down! in the life of activity in Android, we often need to open another activity, another interface. This may occur very briefly, and then back to the main interface. However, these two activity have experienced a certain stage of life in their own life cycle.
The project included relevant content in the previous project for demonstration purposes. The project structure is as follows:
The first step: Add another interface layout in layout, namely Activity_sub.xml, the code content such as the following:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" android:layout_width= "Match_ Parent " android:layout_height=" match_parent "> <textview android:id=" @+id/text1 " android: Layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/hello_world"/ > <button android:id= "@+id/close" android:layout_below= "@id/text1" android:layout_ Width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/close" /> </RelativeLayout>
Of course, the main interface activity_sub.xml content once again layout. The code is as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " > <textview android:id= "@+id/text1" android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:text= "@string/hello_world"/> <button android:id= "@+id/open" android:layout_below= "@id/text1" android:layout_width= "wrap_content" android:layout_height= " Wrap_content " android:text=" @string/open " /></relativelayout>
The second step: Write the code content in the Strings.xml under the Values folder for example the following:
<?XML version= "1.0" encoding= "Utf-8"?><resources> <string name= "App_name" >testactivity</ string> <string name= "Hello_world" >test Activity life cycle</string> <string name= "Close" > Close </string> <string name= "Open" > Open another interface </string></resources>
Note: The contents of this file are what is used in the layout interface, such as android:text= "@string/open" Open is the corresponding <string name= "Open" > Open another interface </string> When the interface is finished, the Program Interface button will appear: Open another interface
Step three: Create a new file Subactivity.java in SRC and write code such as the following:
Package Com.wyz.testactivity;import Android.app.activity;import Android.os.bundle;import android.util.Log;import Android.view.menu;import Android.view.menuitem;import Android.view.view;import Android.view.View.OnClickListener; Import Android.widget.button;public class Subactivity extends Activity {private static final String tag= "subactivity"; overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.LAYOUT.ACTIVITY_SUB); LOG.D (TAG, "life cycle-creation phase (onCreate)"); Button Btn_close = (button) Findviewbyid (r.id.close); Btn_close.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {SubActivity.this.finish ();//Close}}); protected void OnStart () {Super.onstart (); LOG.D (TAG, "life cycle-start phase (OnStart)");} protected void Onresume () {super.onresume (); LOG.D (TAG, "life cycle-recovery phase (Onresume)");} protected void OnPause () {super.onpause (); LOG.D (TAG, "life cycle--Pause creation phase (OnPause)");} protected void OnStop () {super.onstop (); LOG.D (TAG, "life cycle-stop creation phase (onStop)");} Protected void Onrestart () {Super.onrestart (); LOG.D (TAG, "life cycle-restart phase (Onrestart)");} protected void Ondestory () {Super.ondestroy (); LOG.D (TAG, "life cycle-destruction phase (ondestory)");}}
then we have to change the next Mainactivity.java so that it can open the sub-interface:
Package Com.wyz.testactivity;import Android.app.activity;import Android.content.intent;import android.os.Bundle; Import Android.util.log;import Android.view.view;import Android.view.view.onclicklistener;import Android.widget.button;public class Mainactivity extends Activity {private static final String tag= "mainactivity"; overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); LOG.D (TAG, "life cycle-creation phase (onCreate)"); Button Btn_open = (button) Findviewbyid (R.id.open); Btn_open.setonclicklistener (new Onclicklistener () {@ overridepublic void OnClick (View v) {startactivity (new Intent (Mainactivity.this, Subactivity.class));//Open Another Interface}}); protected void OnStart () {Super.onstart (); LOG.D (TAG, "life cycle-start phase (OnStart)");} protected void Onresume () {super.onresume (); LOG.D (TAG, "life cycle-recovery phase (Onresume)");} protected void OnPause () {super.onpause (); LOG.D (TAG, "life cycle--Pause creation phase (OnPause)");} protected void OnStop () {super.onstop (); LOG.D (TAG, "life cycle--stopCreation phase (OnStop) ");} protected void Onrestart () {Super.onrestart (); LOG.D (TAG, "life cycle-restart phase (Onrestart)");} protected void Ondestory () {Super.ondestroy (); LOG.D (TAG, "life cycle-destruction phase (ondestory)");}}
last step: Change the next androidmanifest.xml, add the following in front of the </application> node, for example:
<activity android:name= ". Subactivity " android:label=" @string/app_name ">
The role of this join IS: Let the program know that there is another interface exists!
Note: Android:name is the class name of the sub-interface
Interface effects such as the following:
"Android Dev-8" life cycle, open another activity in activity