This article mainly describes:
1. Identify the system to kill the exit or the user actively quit
2. Distinguish between new creation or system resiliency
1. Identify the system to kill the exit or the user actively quit
When a component loses focus, it is possible for the system to kill the component in order to release the resource, at which point the system calls the Onsaveinstancestate method to notify the store of some state data. If the user presses the back key or executes the context.finish () somewhere in the code, the component exits without going to the Onsaveinstancestate method.
So it is possible to determine whether the component is killed by the system or if the user exits voluntarily, depending on the Onsaveinstancestate method.
2. Distinguish between new creation or system resiliency
Similarly, if the system kills the component, then if the component is to be restored in the future (that is, the component is rebuilt, such as rotating the screen), then the bundle data savedinstancestate in OnCreate is not empty. Conversely, if the newly created component, the bundle must be empty.
So when writing an activity, in oncreate to deal with whether the component is newly created or a system recovery, this is different from the subsequent logic.
For example, a mail compose message:
@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); if(Savedinstancestate! =NULL) {LastState= Savedinstancestate.getboolean ("* * *"); //Restore Last View}Else{String Action=getintent (). Getaction (); if(Intent.ACTION_SEND.equals (ACTION) | | Intent.ACTION_SENDTO.equals (ACTION) | |Intent.ACTION_VIEW.equals (ACTION)||Intent.ACTION_SEND_MULTIPLE.equals (ACTION)) { //send email, then parse the sender name or text content from intent. Init the email sending view } } }
How to differentiate the types of interface component creation and destruction in Android