Bundle Class: Vertical screen of activity to the activity of the horizontal screen, will be the activity of vertical screen to kill the activity of the horizontal screen to create, vertical screen activity will have some calculation results, can be stored with data, stored in memory, Then there is the activity of the horizontal screen. Intent class Putextra (private Bundle Mextras; Bundle is a map,
Packagecom.sxt.day04_03;Importjava.io.IOException;ImportJava.io.InputStream;Importjava.net.MalformedURLException;ImportJava.net.URL;Importjava.net.URLConnection;Importandroid.app.Activity;ImportAndroid.os.Bundle;ImportAndroid.util.Log; Public classMainactivityextendsActivity {String musers; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); LOG.I ("Main", "OnCreate ()"); if(savedinstancestate==NULL){//If you create mainactivity for the first time, empty//Download the Users.json file from the serverdownloadusers (); }Else{//Musers=savedinstancestate.getstring ("Users"); LOG.I ("Main", musers); } } //Download the Users.json file from the server Private voiddownloadusers () {NewThread () {//SDK starting from 4.0 requires that the download data must be executed in the worker thread Public voidrun () {InputStream in=NULL; Try{URL URL=NewURL ("Http://10.0.2.2/users.json"); URLConnection Conn=url.openconnection (); Inch= Conn.getinputstream ();//Create an input stream byte[] buffer=New byte[1024];//The JSON file is less than 1k, and if it is large, it reads 10k at a time. intLen =in.read (buffer); Musers=NewString (buffer, 0, Len); LOG.I ("Main", musers); } Catch(malformedurlexception e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); }finally{ if(in!=NULL){ Try{in.close (); } Catch(IOException e) {e.printstacktrace (); } } } }; }.start (); } @Overrideprotected voidOnStart () {Super. OnStart (); LOG.I ("Main", "OnStart ()"); } @Overrideprotected voidOnresume () {Super. Onresume (); LOG.I ("Main", "Onresume ()"); } @Overrideprotected voidOnPause () {Super. OnPause (); LOG.I ("Main", "OnPause ()"); } @Overrideprotected voidOnStop () {Super. OnStop (); LOG.I ("Main", "OnStop ()"); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); LOG.I ("Main", "OnDestroy ()"); } @Overrideprotected voidOnrestart () {Super. Onrestart (); LOG.I ("Main", "Onrestatr ()"); } @Override//Save activity State data, data in memory, bundle type stored protected voidonsaveinstancestate (Bundle outstate) {Super. Onsaveinstancestate (outstate); LOG.I ("Main", "Onsaveinstancestate ()"); Outstate.putstring ("Users", musers); } @Override//recovering in-memory data protected voidonrestoreinstancestate (Bundle savedinstancestate) {Super. Onrestoreinstancestate (savedinstancestate); LOG.I ("Main", "Onrestoreinstancestate ()"); }}
Timing of onsaveinstancestate and onrestoreinstancestate triggering
When an activity becomes "easy" to be destroyed by the system, the activity's onsaveinstancestate is executed unless the activity is actively destroyed by the user, such as when the user presses the back key. Notice the double quotes above, what is "easy"? The implication is that the activity has not been destroyed, but merely a possibility. What are the possibilities? By overriding the OnXxx method of all life cycles of an activity, including the Onsaveinstancestate and Onrestoreinstancestate methods, we can clearly know when an activity (assumed activity A) When the Onsaveinstancestate method is executed at the top of the current task, there are several cases:1When the user presses the home key. It is obvious that the system does not know how many other programs to run after you press home, and it is not known whether activity A will be destroyed, so the system will call Onsaveinstancestate, giving users the opportunity to save some non-permanent data. The following are some of the conditions in which the analysis follows this principle2, press and hold the home key, and choose to run other programs. 3, press the power button (turn off the screen display). 4, starting a new activity from activity a. 5, such as when switching from the vertical screen to the horizontal screen. Before the screen is switched on, activity A is destroyed and activity A is automatically created after the screen switch, so onsaveinstancestate is bound to be executed. In summary, Onsaveinstancestate's invocation follows an important principle that when the system destroys your activity "without your permission", then Onsaveinstancestate is called by the system, which is the responsibility of the system, Because it has to provide an opportunity for you to save your data (of course you don't save it then just do it yourself). As for the Onrestoreinstancestate method, it should be noted that the Onsaveinstancestate method and the Onrestoreinstancestate method "not necessarily" are called in pairs, Onrestoreinstancestate is invoked only if activity a is "really" destroyed by the system, and if it is just stuck in a situation where there is such a possibility, then the method will not be called, for example, when activity A is being displayed, The user presses the home key back to the main interface, and then the user returns to activity a, in which event A is generally not destroyed by the system for memory reasons, so activity A's Onrestoreinstancestate method will not be executed. In addition, Onrestoreinstancestate's bundle parameters are also passed to the OnCreate method, and you can choose to do a data restore in the OnCreate method.
Data preservation and recovery for Android activity crossbar switch