Android:onnewintent () trigger mechanism and precautions

Source: Internet
Author: User

First, Onnewintent ()

In Intentactivity, override the following methods: OnCreate onStart onrestart onresume onPause onStop OnDestroy onnewintent


1, other application hair intent, do the following methods:
OnCreate
OnStart
Onresume

Method of Hair Intent:

Uri uri = uri.parse ("philn://blog.163.com"new  Intent (Intent.action_view, URI);   StartActivity (it);

2, receive intent statement:

<activity android:name= ". Intentactivity "android:launchmode=" Singletask " android:label=" @string/testname ">    < intent-filter>        <action android:name= "Android.intent.action.VIEW"/>                <category android:name= " Android.intent.category.DEFAULT "/>                <category android:name=" Android.intent.category.BROWSABLE "/>                <data android:scheme= "Philn"/>    </intent-filter></activity>

3, if the intentactivity is at the top of the task stack, that is, the previously opened activity, now in the OnPause, OnStop state, other applications to send intent, the order of execution is:
Onnewintent,onrestart,onstart,onresume.

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:

<activityandroid:label="@string/app_name"android:launchmode="singleTask"android:name="Activity1"></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:

protected void onnewintent (Intent Intent) {   superonnewintent (Intent);   Setintent (intent); // must store the new intent unless getintent () would return the old one   processextradata ();}

Don't 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 onCreate and onnewintent method, call the same method that processes the data, as follows:

 Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);           Setcontentview (R.layout.main);     Processextradata (); }protected voidonnewintent (Intent Intent) {Super. Onnewintent (Intent);      Setintent (Intent); Processextradata ()}Private voidProcessextradata () {Intent Intent=getintent (); //Use the data received here} 

Ii. onnewintent () setintent () and Getintent ()

@Override protected void onnewintent (Intent Intent) {    superonnewintent (Intent);          // setintent (intent);          int data = Getintent (). Getintextra ("HAHA", 0);     // int data = Intent.getintextra ("HAHA", 0);}

If Setintent (intent) is not called, the data obtained by Getintent () will not be what you expect. But using intent.getinxxx seems to get the right results.

Note this remark:
Note that getintent () still returns the original Intent. You can use the Setintent (Intent) to update the IT to this new Intent.

So it is best to call setintent (intent) so that there is no problem when using getintent ().

Android:onnewintent () trigger mechanism and precautions

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.