"Android Dev-8" life cycle, open another activity in activity

Source: Internet
Author: User

Preface: There are many people in life accompany themselves through a journey of a lifetime, but some people just appear at some stage, some people are accompanied by their own for a long time. Just like elementary school, middle school, High school, university, those who once thought will have long time, when experienced the end of the world busy life, or wishful, or frustrated, and gradually those pictures only left memories. 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 for a short time and then back to the main interface. But these two activity have experienced a certain stage of life in their own life cycle.


This project adds relevant content to the previous project and demonstrates the project structure as follows:


The first step: Add another interface layout in layout, namely Activity_sub.xml, with the following code:

<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 re-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>

Step Two: Write the following code in the Strings.xml in the values directory:

<?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 the contents of the layout interface, such as android:text= "@string/open" in the Open is the corresponding <string name= "Open" > Open another interface </string> After the interface layout is finished, the Program Interface button will appear: Open another interface


Step three: Create a new file Subactivity.java in SRC and write the following code:

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 will also modify 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: Modify the next androidmanifest.xml, and add the following before the </application> node:

<activity              android:name= ". Subactivity "              android:label=" @string/app_name ">   

This added function is: Let the program know that there is another interface exists!

Note: Android:name is the class name of the sub-interface


The interface effect is as follows:





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.