activity of reconstruction

activity of reconstruction:
in the background activity may be due to insufficient memory or system resources due to the long time in the stopped state is not used, it may be due to low activity priority is to destroy the system, some of the activity of the state information system will record, by storing a set of key-value pairs in the bundle instance some data records, i.e. "instance state", such as text information input in the EditText each view.
the onPause () and the onSaveInstanceState () except that: onPause () method for data persistence saved, and the onSaveInstanceState () method is used to save temporary state activity.
In order to save more need to restore the state information, developers need to rewrite onSaveInstanceState () callback method, when the activity is accidentally destroyed, the system will bundle the parameter callback IOnSaveInstanceState () method. When the system needs to be rebuilt this activity, the object will be passed to the bundle Oncreate () method and onRestoreInstanceState () method, the activity is restored to the state before destroyed.

Will be called onSaveInstanceState () Under what circumstances do?

(1) pressing the home key

(2) press home button to select another program running

(3) off the display when the

(4) to start a new activity from one activity in

(5) When the screen state switch

Rewriting the onSaveInstanceState () as:

public class DemoActivity extends Activity{

private String userName;
.....
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString("name",userName);
}
}

There are two ways to rebuild the activity: The first is through onCreate (bundle) reconstruction. When creating a new activity or rebuild activity is an existing instance state of preservation, onCreate () will be called when onSaveInstanceSate () is called after the instance state will save a bundle passed to onCrate (), so first determine bundle is empty, then a state of recovery.
public class DemoActivity extends Activity {

the userName String Private;
.....
@Override
protected void the onCreate (the Bundle savedInstanceState) {
    super.onCreate (savedInstanceState);
    the setContentView (R.layout.activity_more_record);
   IF (! savedInstanceState = null) {
         the userName = savedInstanceState.getString ( " name ");
   }
    ...
 }
...
}
second is called after onRestoreInstanceState by () method to resume state. And onCreate () method is different, onRestoreInstanceSate () method of the instance state in the presence of data (i.e., the onSaveInstanceState () call over when activity is destroyed), onRestoreInstanceState () will be called, so there is no longer determines whether the bundle air.
public class DemoActivity extends Activity {

private String userName;
.....

 @Override
 protected void onRestoreInstanceState (savedInstanceState the Bundle) {
      super.onRestoreInstanceState (savedInstanceState);
      the userName = savedInstanceState.getString ( "name");
 }
}
Note that: always called onSaveInstanceState super class () and onRestoreInstaceState () performs default operation to ensure that the state information View architecture preserved.

Guess you like

Origin www.cnblogs.com/irving-zq/p/11407633.html