[Original] Detailed explanation of Android Activity onNewIntent()

Reading Difficulty
: Medium Prerequisites :
1. You need to understand the life cycle of Android, the trigger timing and function of each method.
2. Need to understand the launchMode mode and function of Activity.
3. Intent basic knowledge and role.

The life cycle of an Android Activity is as follows (the picture comes from the Android official website https://developer.android.com/guide/components/images/activity_lifecycle.png ):

That is to say, when the Activity is started for the first time, the calling sequence is as follows:
onCreate() -> onStart() -> onResume()

So, when is onNewIntent() triggered, and what is its use?
Let's look at another picture:

the above picture intuitively shows when onNewIntent() is called. And it also shows the most important point: onCreate() and onNewIntent() will not be called at the same time.

Next, let's take a look at how the official website explains onNewIntent() ( https://developer.android.com/reference/android/app/Activity.html#onNewIntent(android.content.Intent) ):
quote
onNewIntent

added in API level 1
void onNewIntent (Intent intent)
This is called for activities that set launchMode to "singleTop" in their package, or if a client used the FLAG_ACTIVITY_SINGLE_TOP flag when calling startActivity(Intent). In either case, when the activity is re-launched while at the top of the activity stack instead of a new instance of the activity being started, onNewIntent() will be called on the existing instance with the Intent that was used to re-launch it.

An activity will always be paused before receiving a new intent, so you can count on onResume() being called after this method.

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

Parameters
intent Intent: The new intent that was started for the activity.

A brief description: If the launchMode of the Activity is set to "singleTop" mode in AndroidManifest.xml, or the FLAG_ACTIVITY_SINGLE_TOP flag is set when calling startActivity(Intent)
, then when the Activity is started again, if it is still When it exists in the Activity stack and is just at the top of the stack, it will not be recreated, but the original instance will be used directly. At this time, onNewIntent(Intent) will be called, and other in the subsequent life cycle method, you can use the new Intent parameter passed by onNewIntent(Intent). (That is, other methods can use the updated Intent parameter)

That is, the calling sequence is as follows: onNewIntent
() -> onRestart() -> onStart() -> onResume()

(Intent), if you do not call the setIntent(Intent) method to update the Intent, then the original value is still obtained when the getIntent() method is called later.

In the actual process of writing code, we often need to do some of the same processing in onCreate() and onNewIntent(), so we can do it like this:
@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate (savedInstanceState);
     setContentView(R.layout.activity_schedule);
     // Do what you want.
     onNewIntent(getIntent());
     // Do others
}
If you don't want to call onNewIntent() directly in the onCreate() method, you can also call a custom method in both onCreate() and onNewIntent() to achieve the same purpose:
@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate (savedInstanceState);
     setContentView(R.layout.activity_schedule);
     // Do what you want.
     sameProcess(getIntent());
     // Do others
}

@Override
protected void onNewIntent(Intent intent) {
     // Do what you want.
     sameProcess(intent);
     // Do others
}

Finally, to sum up, when starting Activtiy, if you want to operate the Intent without onCreate() being triggered, you need to use onNewIntent().

references:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326231206&siteId=291194637