English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Trigger Mechanism and Points for Attention of Android onNewIntent()

First, onNewIntent()

In IntentActivity, override the following methods: onCreate, onStart, onRestart, onResume, onPause, onStop, onDestroy, onNewIntent

1Other applications send Intent, execute the following methods:

onCreate
onStart
onResume

Method to send Intent:

Uri uri = Uri.parse("philn:",//blog.163.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);  
startActivity(it);

2Receiving 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>

3If the IntentActivity is at the top of the task stack, that is, the Activity that was opened before and is now in the onPause, onStop state, if other applications send Intent, the execution order is:

onNewIntent, onRestart, onStart, onResume.

When developing Android applications, it is very simple to start another Activity from an Activity and pass some data to the new Activity, but when you need to bring an Activity running in the background back to the foreground and pass some data, there may be a slight problem.

Firstly, by default, when you start an Activity through an Intent, even if there is already a running Activity with the same name, the system will still create a new Activity instance and display it. To prevent the Activity instance from being instantiated multiple times, we need to configure the loading mode (launchMode) of the activity in the AndroidManifest.xml to achieve single-task mode, as shown below:

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

When the launchMode is set to singleTask, an Activity is started through an Intent. If the system already has an instance, the system will send the request to this instance, but at this time, the system will not call the usual onCreate method to handle the request data, but will call the onNewIntent method, as shown below:

protected void onNewIntent(Intent intent) {
 super.onNewIntent(intent);
 setIntent(intent);//must store the new intent unless getIntent() will return the old one
 processExtraData();
}

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

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() will return the old one
 processExtraData()
}
private void processExtraData(){
 Intent intent = getIntent();
 //use the data received here
}

Second, onNewIntent() setIntent() and getIntent()

@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 called, the data obtained by getIntent() will not be what you expect. However, it seems that you can obtain the correct result by using intent.getInXxx.

Note 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 best to call setIntent(intent) so that there will be no problem when using getIntent().

This is the compilation of the material on the triggering mechanism and precautions of Android onNewIntent(), and more relevant materials will be supplemented in the future, thank you for your support to this site!

Statement: The content of this article is from the Internet, the copyright belongs to the original author, the content is contributed and uploaded by Internet users spontaneously, this website does not own the copyright, has not been manually edited, and does not assume relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace # with @ when sending an email to report abuse, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.

You May Also Like