Acitivity data transfer processing in Singletask load mode

Source: Internet
Author: User

in thein Android development, we typically pass data between different pages, such asfromActivityaStartActivityband pass the data toActivityb inside, this is very simple, with INTETN add bundle can. But sometimes if youneed to let the background runActivityback to the front desk and pass the data there are some problems.

Usually, we passIntentKai toActivity, even if there is an identical runningActivity,the system will create a newActivityinstance and display it. At this point we'd better let the activity not start many times,through theAndroidmanifest.xmlConfigurationActivitythe load mode (Launchmode) to implement single-task mode (android:launchmode= "singleTask")。Singletaskthe time,start to aActivitya,if the system already exists Activitya instance, the system will start Activitya directly. At this point, however, the system will not invoke the usual cases where we process the request data.onCreatemethod instead of invoking theonnewintentmethod, as follows:


protected void Onnewintent (Intent Intent) {

Super.onnewintent (Intent);

Setintent (intent);
//Processing data, data=getintent (). Get ... ..

}Sometimes, however, the system may be killed in the background at any time. Activity , the system will also invoke the Activitya when it starts onCreate method without calling the onnewintent method. How do we deal with this at this time? is usually in onCreate and the onnewintent method, you can resolve the problem by invoking the same method that processes the data.

Attention:

protected void Onnewintent (Intent Intent) {

super.onnewintent (intent);

setintent (intent);

Process data, data=getintent (). Get .....

}

in the above method, if Setintent (intent) is not called, the data obtained by Getintent () will not be what you expect. so it's best to call setintent (intent) so that you can call Getintent () to get the data.

Acitivity data transfer processing in Singletask load mode

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.