Android onnewintent() trigger mechanism and precautions

1、 Onnewintent ()

Override the following methods in intentactivity: oncreate OnStart onrestart onresume onpause onstop ondestroy onnewintent

1. Other applications send intent and perform the following methods:

onCreate onStart onResume

Method of sending intent:

2. Receive intent statement:

3. If the intentactivity is at the top of the task stack, that is, the previously opened activity is now in onpause and onstop status, and other applications send intents, the execution order is:

onNewIntent,onRestart,onStart,onResume。

During Android application development, it is very easy to start another activity from one activity and transfer some data to the new activity, but there may be a little problem when you need to return the activity running in the background to the foreground and transfer some data.

First, by default, when you start an activity through intent, the system will create a new activity instance and display it even if the same running activity already exists. In order not to instantiate the activity multiple times, we need to XML to configure the launch mode of the activity to implement the single task mode, as shown below:

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

When the launchmode is singletask, start an activity through intent. If an instance already exists in the system, the system will send the request to the instance. However, at this time, the system will no longer call oncreate method, which normally handles the request data, but onnewintent method, as shown below:

Do not forget that the system may kill the Activity running in the background at any time. If all of this happens, then the system will call the onCreate method instead of calling the onNewIntent method. A good solution is to call the same data processing method in onCreate and onNewIntent methods as follows:

2、 Setintent() and getintent() of onnewintent()

If setintent (intent) is not called, the data obtained by getintent () will not be what you expect. But use intent Getinxxx, it seems that you can get the correct result.

Pay attention to this sentence:

Note that getIntent() still returns the original Intent. You can use setIntent(Intent) to update it to this new Intent.

Therefore, it is better to call setintent (intent), so that there will be no problem when using getintent ().

The above is the data sorting of Android onnewintent() trigger mechanism and precautions. We will continue to supplement relevant data in the future. Thank you for your support for this site!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>