Create a Intenta and INTENTB two activity and override the following methods:
OnCreate onStart onrestart onresume onPause onStop OnDestroy onnewintent
First, Intenta application hair intent, jump to INTENTB, do the following methods:
i/@@ zzfcthotfixz (12410): OnCreate
i/@@ zzfcthotfixz (12410): OnStart
i/@@ zzfcthotfixz (12410): Onresume
i/@@ zzfcthotfixz (12410): OnPause
Method of Hair Intent:
Intent intent=new Intent ();
Intent.setclass (Getapplicationcontext (), intentb.class);
StartActivity (Intent);
Second, receive intent statement:
<activity android:name= ". Intentb "android:launchmode=" Singletask "
Android:label= "@string/testname" >
</activity>
Third, if the INTENTB is at the top of the task stack, that is, the activity that was previously opened, is now in
i/@@ zzfcthotfixz (12410): OnPause or OnStop State,Other applications Send intent start B, the order of execution is:
i/@@ zzfcthotfixz (12410): Onnewintent
i/@@ zzfcthotfixz (12410): Onrestart
i/@@ zzfcthotfixz (12410): OnStart
i/@@ zzfcthotfixz (12410): Onresume
Description: When developing an Android application, it is very easy to start another activity from one activity and pass some data to the new activity. But when you need to get the back-office activity back to the foreground and pass some data, there might be a little bit of a problem.
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:
<activity android: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) {
Super.onnewintent (Intent);
Setintent (intent);//must Store the new intent unless getintent () would return the old one
Processextradata ();
}
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:
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 () would return the old one
Processextradata ()
}
private void Processextradata () {
Intent Intent = Getintent ();
Use the data received here
}
Note: Update intent with setintent so that Getintent gets the latest intent.
Android:launchmode= "Singletask" and Onnewintent (Intent Intent) two properties,
Now summarize the experience:
Android:launchmode= "Singletask" is configured in Mainifest, which guarantees that the activity in the stack is always only one, no matter how many times you start it;
onnewintent (Intent Intent) is the parent-class method of the override activity .
The code is as follows:
Manifest.xml
<activity android:name= ". Onnewintentdemo "android:launchmode=" Singletask "android:label=" @string/app_name "> <intent-filter> <action android:name= "Android.intent.action.MAIN"/> <category android:name= "Android.intent.category.LAUN CHER "/> </intent-filter> <intent-filter> <action android:name=" Android.intent.action.VIEW "/ > <category android:name= "Android.intent.category.DEFAULT"/> <data android:mimetype= "video/*"/> </intent-filter> </activity>
In Activity
@Override
protected void Onnewintent (Intent Intent) {
if (DEBUG) log.i (TAG, "onnewintent ~~~~~~~ intent =" +intent);
Super. Onnewintent (Intent);
}
Java Code@Override protected void Onnewintent (Intent Intent) {if (DEBUG) log.i (TAG, "onnewintent ~~~~~~~ Intent =" +inte NT); Super.onnewintent (Intent); }
Note: When you press the home key to exit, and then long press the home key to enter, at this time onnewintent not be accessed, because the time to enter again is not initiated intent.
Android onnewintent Call Timing