What's in this blog? Hide
- Introduction: Android Intent Flags
- How to Set Android Intent Flags
- Type of Android Intent Flags
Android Intent Flags are used to signify the state or type of an Android Intent. Android Intent flags are set using the Android Context.putExtra() method and can be retrieved using the getFlags() method on the Android intent object.
They allow you to be very specific about the actions you want your app to take and can help avoid ambiguity. There are different types of Android Intent Flags which will be explained in this blog post with examples.
Introduction: Android Intent Flags
Android Intent Flags are a set of special bits that you can set on an Intent to change its behavior. They allow you to control things like whether the Intent should start a new activity, or whether it should return results.
How to Set Android Intent Flags
You can set an Android Intent Flag by using the Android Context.putExtra() method. This method takes two arguments: the flag you want to set, and a value (usually true or false) that indicates whether the flag should be set.
Type of Android Intent Flags
If set, this activity will become the start of a new task on this history stack. A task (from the activity that started it to the next task activity) is a sequence of activities that share the same root activity. This flag is generally used by activities that provide a “home” button to the task, such as an email application.
If set, and the activity being launched is already running in the current task, then instead of launching a new instance of that activity, all of the other activities on top of it will be closed and this Intent will be delivered to the (now on top) old activity as a new Intent. (e.g. document browsing where clicking on a listed document would lead to viewing that document, possibly in another activity).
If FLAG_ACTIVITY_NEW_TASK is also set, the task containing this activity will be brought to the foreground and given focus instead of being created from scratch. Otherwise, a new task will be created for it. This flag can not be used when the Intent’s target is within your own task; if so, no changes will happen (and you will receive an “IllegalArgumentException”).
If set, the system will not create a new instance of the activity if it is already running at the top of the current task. However, if an instance would be created in a separate task, that instance is still brought to the front and receives the new intent, resulting in two instances running side-by-side in different tasks.
This is generally used only with activities that are part of a tabbed UI; using this flag with those activities allows each tab to have its own unique history stack maintained by Android, rather than having all tabs share one history stack. This flag is not set by default.
If set in an Intent passed to Context.startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are destroyed. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK; if it is not set, then a new instance of the activity will be added to an existing task as usual.
If it is set, and Android encounters the same class when restoring a task, it will instantiate a new instance of that class and deliver the Intent to that instance through its onNewIntent() method (instead of simply delivering the Intent to the existing instance as is normal behavior).
This means you must set this flag when you want a new instance created; if not set, Android may instead use your existing Activity. Note also that this flag will cause any activities under the old task root to be destroyed when the last activity in them is finished or becomes empty (due to back navigation away from them), since they are no longer part of an active task.
Added in API level 21. If set, and this activity is launching in a new task, the system will not launch any activities in between the caller and this one. In other words, a new activity stack will be generated for this one through FLAG_ACTIVITY_TASK_ON_HOME. This is like saying all of those launched activities are “transparent” (they aren’t really; Android is just not going to keep track of them). Then, when the user presses Back from this activity, they will go back to the home screen, with no trace of the called-upon activity remaining.
Added in API level 21. If set, and this activity is launching in a new task, the system will launch the top activity in an existing task with the same Affinity as if FLAG_ACTIVITY_NEW_TASK was used.
That is, Android will look for an existing task with the same affinity to re-use before it creates a new one. This is useful when activities are part of a document flow, where some activities should only exist within that document (e.g. details pages) and others should be reachable from anywhere else within the app (e.g. browse or search).
Note that this flag must be set on all activities that can appear under these circumstances; setting it on just one activity in a task may result in lost user data as Android fails to correctly restore state when going back.
Learn more about Intent Service in android from link here
Added in API level 11. If set in an Intent passed to Context::startActivity(), this flag will cause any existing task that would be associated with the activity to be cleared before the activity is started. That is, the activity becomes the new root of an otherwise empty task, and any old activities are destroyed to make way for it. This can only be used in conjunction with FLAG_ACTIVITY_NEW_TASK; if it is not set, then a new instance of the activity will always be created (as normal) even if there is an existing task running.
Added in API level 11. If set in an Intent passed to Context::startActivity(), this flag indicates that the caller wants the activity to be launched as if it was launched from history (its task with all previous activities will be re-created and the activity will be placed on top). That is, generally speaking a new instance of the activity will not be created. This flag can not be used when the Intent’s target is within your own task; if so, no changes will happen (and you will receive an “IllegalArgumentException”).
Android Intent Flags are powerful tools that can help you control the behavior of your app. Use them wisely to avoid ambiguity and to get the most out of your Android app development!