When the activity in the background is automatically reclaimed by the system due to the memory tension, he will re-invoke Oncretae () and then lose the previous state before it was placed in the background.
This is the time to rewrite the activity of the two methods to save and restore the state, for example: You are editing a text message, this time a phone call, after the call back to the text interface,
SMS is just being re-restarted by the system, then half of the original edited content can not be lost, it affects the user experience. So the solution is as follows:
1 Private Static FinalString instance_status= "Instance_status";//Parent Class State2 Private Static FinalString status_alpha= "Status_alpha";//user-defined state3 4 @Override5 protectedparcelable onsaveinstancestate () {6Bundle bundle=NewBundle ();//Create a new bundle7Bundle.putparcelable (Instance_status,Super. Onsaveinstancestate ());//Save the parent class state to the bundle8Bundle.putfloat (Status_alpha,malpha);//saving user data to bundles9 returnBundle//return BundleTen } One A @Override - protected voidonrestoreinstancestate (parcelable state) { - if(stateinstanceofBundle) {//determines whether the currently received state is a subclass of the bundle theBundle bundle= (bundle) state;//Type Casting -Malpha=bundle.getfloat (Status_alpha);//Remove user-defined state - Super. Onrestoreinstancestate (Bundle.getparcelabel (instance_status));//Restore parent class State - return;//here to return, otherwise if content is executed two times + } - Super. Onrestoreinstancestate (state); +}
Storage and recovery of activity status in Android development