Android onnewintent () trigger mechanism and precautions _android

Source: Internet
Author: User

One, Onnewintent ()

Override the following methods in Intentactivity: OnCreate onStart onrestart onresume onpause onStop OnDestroy

1, other applications to send intent, to do the following methods:

OnCreate
OnStart
Onresume

How to send intent:

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

2, receive intent declaration:

<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 activity previously opened, now in OnPause, OnStop state, and other applications to send intent, the order of execution is:

Onnewintent,onrestart,onstart,onresume.

When Android applications are developed, it's easy to start another activity from one activity and pass some data to the new activity, but there may be a small problem when you need to get back to the foreground and deliver some data to the activity that runs in the background.

First, by default, when you go through intent to an activity, even if there is already an identical running activity, the system creates a new activity instance and displays it. In order to not allow the activity to be instantiated multiple times, we need to configure the Androidmanifest.xml load mode (Launchmode) to implement the single task pattern, as follows:

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

When Launchmode is Singletask, a intent is set to an activity, and if an instance of the system already exists, the system sends the request to the instance, but this time, Instead of calling the OnCreate method that normally handles the request data, the system calls the Onnewintent method as follows:

protected void Onnewintent (Intent Intent) {

 super.onnewintent (Intent);

 Setintent (intent);//must Store the new intent unless getintent () 'll return to the old one

 processextradata ();

}

Do not forget that the system may at any time kill the activity in the background, if all this happens, then the system will call the OnCreate method, without invoking the Onnewintent method, a good solution is in onCreate and onnewintent method to call the same method of processing data, as follows:

public void OnCreate (Bundle savedinstancestate) {

 super.oncreate (savedinstancestate);

 Setcontentview (r.layout.main);

 Processextradata ();

}
protected void Onnewintent (Intent Intent) {

 super.onnewintent (Intent);

  Setintent (intent);//must Store the new intent unless getintent () 'll return to the old one

 processextradata ()

}
   private void Processextradata () {

 Intent Intent = Getintent ();

 Use the data received here

}
 

Ii. setintent () and Getintent () of Onnewintent ()

@Override
protected void onnewintent (Intent Intent) {
 super.onnewintent (Intent);

 Setintent (intent);

 int data = Getintent (). Getintextra ("HAHA", 0);
 int data = Intent.getintextra ("HAHA", 0);
}

If Setintent (intent) is not invoked, then getintent () Gets the data that is not what you expected. But using intent.getinxxx seems to get the right results.

Note this sentence:

Note that getintent () still returns the original Intent. can use Setintent (Intent) to update it to the new Intent.

So it's best to call setintent (intent) so that you don't have a problem with getintent ().

The above is the Android onnewintent () trigger mechanism and attention to the data collation, follow-up continue to supplement the relevant information, thank you for your support of this site!

Related Article

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.