How we can use onNewIntent() in any Activity?

Android

Android Problem Overview


What is the real use of onNewIntent() in the activity life cycle and how do we use this method?

Android Solutions


Solution 1 - Android

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).

If you set to single top, the activity will not be launched if it is already running at the top of the history stack. It will not relaunch just show from stack.

Solution 2 - Android

Check this link onNewIntent()

In manifest.xml, in an activity tag set launchmode="singleTask"

Solution 3 - Android

Above answers are incomplete.

In case the activity 'a1' of Application 'A1' has launch mode "singleTask" or "singleTop" and is already alive (in task t1) but paused, and now another task (say Task t2) (usually another android app) sends an intent to activity a1 of application A1, then instead of creating another instance of activity in task t2, android resumes a1 from task t1, by issuing a callback to onNewIntent(intent) method in a1.

Tasks and back stack is an important concept, no blog explains better than android documentation itself.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionneeraj tView Question on Stackoverflow
Solution 1 - AndroidPadma KumarView Answer on Stackoverflow
Solution 2 - AndroidRavikiranView Answer on Stackoverflow
Solution 3 - AndroidJaydevView Answer on Stackoverflow