Onnewintent (Intent Intent): Usage

Source: Internet
Author: User

Reference: http://www.cnblogs.com/shitianzeng/articles/2807062.html

Onnewintent (Intent Intent):

When developing an Android app, it's easy to start another activity from one activity and pass some data to the new activity, but there may be a little bit of a problem when you need to get the background activity back to the foreground and pass some data.

First, by default, when you start an activity with intent, a new activity instance is created and displayed, even if there is already a running activity. To keep the activity from being instantiated multiple times, we need to implement the single-task mode by configuring the activity's Load mode (Launchmode) in Androidmanifest.xml, as follows:

1 <activity android:label= "@string/app_name" android:launchmode= "Singletask" android:name= "Activity1" >

2 </activity>

When the Launchmode is Singletask, the system will send the request to this instance by intent to an activity, if the system already has an instance, but this time, Instead of invoking the OnCreate method, which normally we process the request data, the system calls the Onnewintent method, as follows:

1 protected void Onnewintent (Intent Intent) {

2 super.onnewintent (Intent);

3 setintent (intent);//must Store the new intent unless getintent () would return the old one

4 Processextradata ();

5}


Do not forget that the system may kill the activity in the background at any time, if this happens, then the system will call the OnCreate method, instead of calling the Onnewintent method, A good solution is to call the same method of processing data in the OnCreate and Onnewintent methods as follows:

OnCreate public void (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

Processextradata ();

05}

06

protected void Onnewintent (Intent Intent) {

Super.onnewintent (Intent);

Setintent (Intent)//must store the new intent unless getintent () would return the old one

Ten Processextradata ()

11}

12

private void Processextradata () {

Intent Intent = Getintent ();

//use the data received here

16}

Onnewintent (Intent Intent): Usage

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.