Today's interview was asked a question: when activity A and activity b,activity A were covered by B, which methods were called in the activity life cycle? The activity A, Dialog b dialog box covers what methods were called in the activity a,activity life cycle? Answers to the following announcement:
We often use activity and know that there are OnCreate, OnStart, Onresume, OnPause, OnStop, OnDestroy, Onrestart in the activity life cycle, So when will these methods be called?
First, let's look at the following classic life cycle diagram Flowchart:
I believe many friends have seen this flowchart, but also basic understanding of the activity life cycle:
1. Start an activity: the system calls OnCreate ()--->onstart ()--->onresume () and the activity goes into a running state.
2, the user exits the current activity and closes the app (e.g. click the Back button): The system calls OnPause ()--->onstop ()--->ondestroy (),
3, the current activity is overwritten by other activity or click on the Power key: The system will call OnPause ()--->onstop (); Activity goes into stasis.
4, the current activity to jump to a new activity interface or click the Home button: The system will call OnPause ()--->onstop (); Activity goes into stasis.
5, returning from the previous activity to the current activity: The system calls Onrestart ()--->onstart ()--->onresume (), and the current activity enters the running state.
6, the screen switch: The current activity system will call OnPause ()--->onstop ()--->ondestroy (); Then recreate a new activity:oncreate ()--->onstart ()--->onresume ();
So today's interview question: What happens to the popup dialog life cycle?
7, Popup dialog box (popupwindow,dialog):
At this point the system does not remove activity from the top of the stack or there is another activity covering the current activity. Therefore, the system does not invoke methods in the life cycle of the activity.
Here are some examples to illustrate each situation:
public class Mainactivity extends Activity {private static final String TAG = "mainactivity"; Private Button Mbtnshowdialog; Private Button Mbtnshowpopwindow; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); Mbtnshowdialog = (Button) Findviewbyid (R.id.button); Mbtnshowpopwindow = (Button) Findviewbyid (R.id.btn_show_pop); Mbtnshowdialog.setonclicklistener (listener); Mbtnshowpopwindow.setonclicklistener (listener); LOG.I (TAG, "==oncreate () = ="); }//Click event View.onclicklistener listener = new View.onclicklistener () {@Override public void OnClick (V Iew v) {switch (V.getid ()) {case R.id.button:doshowdialog (); Break Case R.id.btn_show_pop:doshowpopwindow (v); Break } } }; Showdialog box public void Doshowdialog () {Alertdialog.builder Builder = new Alertdialog.builder (this); Builder.settitle ("dialog box"); Builder.setmessage ("dialog box message"); Builder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {@Override public void onclic K (dialoginterface Dialog, int which) {Intent Intent = new Intent (mainactivity.this, Cameraactivity.class); StartActivity (Intent); } }); Builder.show (); }//Show pop window public void Doshowpopwindow (View v) {Popupwindow window = new Popupwindow (); View view = This.getlayoutinflater (). Inflate (R.layout.popup_window, NULL); Window.setcontentview (view); Window.setwidth (100); Window.setheight (100); Window.showasdropdown (v); } @Override public void Onwindowfocuschanged (Boolean hasfocus) {super.onwindowfocuschanged (hasfocus); LOG.I (TAG, "==onwindowfocuschanged==" + hasfocus);} @Override protected void Onrestart () {Super.onrestart (); LOG.I (TAG, "==onrestart=="); } @Override protected void OnStart () {Super.onstart (); LOG.I (TAG, "==onstart () = ="); } @Override protected void Onresume () {super.onresume (); LOG.I (TAG, "==onresume () = ="); } @Override protected void OnPause () {super.onpause (); LOG.I (TAG, "==onpause () = ="); } @Override protected void OnStop () {super.onstop (); LOG.I (TAG, "==onstop () = ="); } @Override protected void OnDestroy () {Super.ondestroy (); LOG.I (TAG, "==ondestroy () = ="); }//This method will be called when the screen is toggled, @Override protected void onrestoreinstancestate (Bundle savedinstancestate) {SUPER.O Nrestoreinstancestate (savedinstancestate); LOG.I (TAG, "==onrestoreinstancestate=="); }//is used primarily to save some state values before the activity loses focus and the activity cannot be displayed before the OnStart method is called before the OnStop method @Override protected void Onsaveinstance StatE (Bundle outstate) {super.onsaveinstancestate (outstate); LOG.I (TAG, "==onsaveinstancestate=="); } @Override public void onconfigurationchanged (Configuration newconfig) {super.onconfigurationchanged (Newcon FIG); LOG.I (TAG, "==onconfigurationchanged=="); }}
Refer to other information: I added onwindowfocuschanged, onsaveinstancestate, Onrestoreinstancestate, onconfigurationchanged method;
1,onwindowfocuschanged method: Called when the current window of the activity gains or loses focus. It is called when the window Gets or loses focus.
(1) When the activity is first created, the window gets to the focus
(2) Click the Home button or click the key lock screen
(3) Show app again
Also, when the user exits the current activity, the current activity is overwritten by other activity and is also called. (It is recommended to mask the method to facilitate subsequent testing)
2,onsaveinstancestate method: Called to retrieve per-instance state from a activity before being killed so this state can B E restored inoncreate (bundle) or onrestoreinstancestate (bundle) saves some state values before the activity is killed by the system. For use in OnCreate or Onrestoreinstancestate methods.
3,onrestoreinstancestate method: This method was called after OnStart () when the activity was being re-initialized from a prev iously saved state, given here in savedinstancestate. is called when the activity is re-initialized.
4,onconfigurationchanged method: Called by the system, the device configuration changes while your component are running. When the device's The configuration is called when it changes.
Test model: Millet 2A
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
[Android Basics] Life cycle of activity