Storage and recovery of activity status in Android development

Source: Internet
Author: User
Tags call back type casting

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.