Android App components activity

Source: Internet
Author: User

I. Introduction of activity

1, activity has a window that can edit the user interface, the window fills the entire screen, can also be less than or floating on the other Windows

2, the application usually has a number of interrelated activity and a mainactivity


Jump between multiple activity simple signals



Second, acitivity life cycleThe activity's declaration cycle refers to all the states that the activity is experiencingActivty'sFull life cycleRefers to starting with the first call to the OnCreate () method until the Ondrestroy () method location is called.Activity's Visual life cyclemeans thatthe call from OnStart () starts until the corresponding OnStop () call ends. during this time, the user can see the activity on the screen. the activity's foreground life cycle refers to the Onresume () call, up to the corresponding OnPause () call. During this time, the activity is at the top of the foreground and interacting with the user.
third, the state of activity preservationCallback this method with the Onsaveinstance () method to reclaim the current activtyCases:
Package Com.songjialongs.myapplication;import Android.os.persistablebundle;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.edittext;public class Activity_lifecycle extends    appcompatactivity {EditText user_name_et;    CheckBox REM_USER_NAME_CB;    Boolean iscckeck = false;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity__lifecycle);        User_name_et = (EditText) Findviewbyid (R.id.user_name_et);        REM_USER_NAME_CB = (CheckBox) Findviewbyid (R.ID.REM_USER_NAME_CB);        Iscckeck = Savedinstancestate.getboolean ("Remname");        Rem_user_name_cb.setchecked (Iscckeck);        Final String name = savedinstancestate.getstring ("name");            Rem_user_name_cb.setoncheckedchangelistener (New Compoundbutton.oncheckedchangelistener () {@Override PuBlic void OnCheckedChanged (Compoundbutton Compoundbutton, Boolean b) {if (b) {User_nam                    E_et.settext (name);                Iscckeck = true;                } else {Iscckeck = false;    }            }        }); } @Override public void Onsaveinstancestate (Bundle outstate, Persistablebundle outpersistentstate) {Super.on        Saveinstancestate (Outstate, outpersistentstate);        String content = User_name_et.gettext (). toString (); Outstate.putstring ("name", content);//parameter is String key,string Value outstate.putboolean ("Remname", iscckeck);//parameter is Stri ng Key,boolean Value}}
Layout code:
<?xml version= "1.0" encoding= "Utf-8"? ><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/user_name_txt" android:layout_width= "Wrap_content"        android:layout_height= "Wrap_content" android:layout_marginleft= "20DP" android:layout_margintop= "20DP"        android:text= "User name:" android:textsize= "32sp"/> <edittext android:id= "@+id/user_name_et"        Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:layout_marginright= "20DP" android:layout_margintop= "20DP" android:layout_torightof= "@id/user_name_txt"/> <checkbox and        Roid:id= "@+id/rem_user_name_cb" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_alignright= "@iD/user_name_et "android:layout_below=" @id/user_name_et "android:text=" Remember Password "android:textsize=" 24SP "/ ></RelativeLayout>


Iv. activity startup mode LaunchmodeLaunchmode is in the process of activity jump, decide whether to generate new activity, whether to reuse existing, and whether to common with other instances of a task stack1. Stardard modeStardard mode is the default startup mode, and each jump activity is startactivty generates a new activity and presses into the system default task stack

Package Com.songjialongs.myapplication.luanchermode;import Android.content.intent;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;import Com.songjialongs.myapplication.r;public Class    Mainactivity extends Appcompatactivity {TextView activty_a_txt;    Button activty_a_btn;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity__main);        Activty_a_txt = (TextView) Findviewbyid (r.id.activty_a_txt);        ACTIVTY_A_BTN = (Button) Findviewbyid (R.ID.ACTIVITY_A_BTN);        Activty_a_txt.settext (This.tostring ()); Activty_a_btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View vie                W) {Intent Intent = new Intent (mainactivity.this, Activity_stardard_b.class);            StartActivity (Intent);}        }); }}


Import Android.content.intent;import Android.support.v7.app.appcompatactivity;import Android.os.Bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;import    Com.songjialongs.myapplication.r;public class Activity_stardard_b extends appcompatactivity {TextView activty_B_Txt;    Button activty_b_btn;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity__stardard);        Activty_b_txt = (TextView) Findviewbyid (r.id.stardard_b_txt);        ACTIVTY_B_BTN = (Button) Findviewbyid (R.ID.STARDARD_B_BTN);        Activty_b_txt.settext (This.tostring ()); Activty_b_btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View vie                W) {Intent Intent = new Intent (activity_stardard_b.this, Mainactivity.class);            StartActivity (Intent);    }        }); }}



2. Singletop modeThe activity to jump is alreadyexistWith the system's task, and is located in theTop of Stack, the same activity is used on top of the stack, and if it does not exist, a new activty is created and pressed into the system default task stack
Package Com.songjialongs.myapplication.luanchermode;import Android.app.activity;import android.content.Intent; Import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;import Com.songjialongs.myapplication.r;public Class    Mainactivity extends Appcompatactivity {TextView activty_a_txt;    Button activty_a_btn;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity__main);        Activty_a_txt = (TextView) Findviewbyid (r.id.activty_a_txt);        ACTIVTY_A_BTN = (Button) Findviewbyid (R.ID.ACTIVITY_A_BTN);        Activty_a_txt.settext (This.tostring ()); Activty_a_btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View vie                W) {Intent Intent = new Intent (mainactivity.this, Activity_singletop_b.class); StartActivity (Intent);    }        }); }}



Package Com.songjialongs.myapplication.luanchermode;import Android.content.intent;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;import Com.songjialongs.myapplication.r;public class Activity    _singletop_b extends Appcompatactivity {TextView singletop_b_btn_txt;    Button singletop_b_btn_btn;    Button Singletop_btoa_btn_txt;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity__singletop__b);        Singletop_b_btn_txt = (TextView) Findviewbyid (r.id.singletop_b_btn_txt);        SINGLETOP_B_BTN_BTN = (Button) Findviewbyid (R.ID.SINGLETOP_B_BTN);        Singletop_btoa_btn_txt = (Button) Findviewbyid (R.ID.SINGLETOP_BTOA_BTN);        Singletop_b_btn_txt.settext (This.tostring ());          Singletop_b_btn_btn.setonclicklistener (New View.onclicklistener () {@Override  public void OnClick (view view) {Intent Intent = new Intent (Activity_singletop_b.this, Activity_singletop                _b.class);            StartActivity (Intent);        }        }); Singletop_btoa_btn_txt.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View view)                {Intent Intent = new Intent (activity_singletop_b.this, Mainactivity.class);            StartActivity (Intent);    }        }); }}


3. Singletask modeTo jump to the activity already exists with the stack, jump directly to the activity; Create a new activity if it does not exist and press the default task stack in the system
Package Com.songjialongs.myapplication.luanchermode;import Android.app.activity;import android.content.Intent; Import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;import Com.songjialongs.myapplication.r;public Class    Mainactivity extends Appcompatactivity {TextView activty_a_txt;    Button activty_a_btn;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity__main);        Activty_a_txt = (TextView) Findviewbyid (r.id.activty_a_txt);        ACTIVTY_A_BTN = (Button) Findviewbyid (R.ID.ACTIVITY_A_BTN);        Activty_a_txt.settext (This.tostring ()); Activty_a_btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View vie                W) {Intent Intent = new Intent (mainactivity.this, Activity_singletask_b.class); StarTactivity (Intent);    }        }); }}



Package Com.songjialongs.myapplication.luanchermode;import Android.content.intent;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;import Com.songjialongs.myapplication.r;public class Activity    _singletask_b extends Appcompatactivity {TextView activty_b_txt;    Button activty_b_btn;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity__singletask);        Activty_b_txt = (TextView) Findviewbyid (r.id.singletask_b_txt);        ACTIVTY_B_BTN = (Button) Findviewbyid (R.ID.SINGLETASK_B_BTN);        Activty_b_txt.settext (This.tostring ()); Activty_b_btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View vie                W) {Intent Intent = new Intent (activity_singletask_b.this, Mainactivity.class); StartactivitY (Intent);    }        }); }}


4. SingleInstance modeJump Acitivity creates a new task stack, presses the jump activity into the task stack, and the newly created task stack has only one activity
Import Android.content.intent;import Android.support.v7.app.appcompatactivity;import Android.os.Bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;import    Com.songjialongs.myapplication.r;public class Mainactivity extends appcompatactivity {TextView activty_a_txt;    Button activty_a_btn;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Activty_a_txt = (TextView) Findviewbyid (r.id.activty_a_txt);        ACTIVTY_A_BTN = (Button) Findviewbyid (R.ID.ACTIVITY_A_BTN);        Activty_a_txt.settext (this.tostring () + ": Stack:" + gettaskid ()); Activty_a_btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View vie                W) {Intent Intent = new Intent (mainactivity.this, Activity_singleinstance_b.class);            StartActivity (Intent);    }        }); }}

Package Com.songjialongs.myapplication.luanchermode;import Android.content.intent;import Android.support.v7.app.appcompatactivity;import Android.os.bundle;import Android.view.view;import Android.widget.button;import Android.widget.textview;import Com.songjialongs.myapplication.r;public class Activity    _singleinstance_b extends Appcompatactivity {TextView singinstance_b_txt;    Button singinstance_b_btn;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (r.layout.activity__singleinstance);        Singinstance_b_txt = (TextView) Findviewbyid (r.id.singleinstance_b_txt);        SINGINSTANCE_B_BTN = (Button) Findviewbyid (R.ID.SINGLEINSTANCE_B_BTN);        Singinstance_b_txt.settext (this.tostring () + ": Stack:" + gettaskid ()); Singinstance_b_btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (Vie W view) {Intent Intent = new Intent (Activity_singleinstance_b.this, Activity_singleinstance_b.class);            StartActivity (Intent);    }        }); }}

Android App components activity

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.