How to differentiate the types of interface component creation and destruction in Android

Source: Internet
Author: User

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

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.