Android tasks and back stack
Intent flags of activity
This time we are going to figure out how to use Intent flag of activity with startActivity().
If you are interested in the demo project, just download the app from the google play and give it a try.

Prerequisites
First, I strongly recommend you read the doc on Android Developer Understand Tasks and Back Stack, it will help us to understand how the back stack works.
And you can also read the previous article about activity:launchmode.
Intent flags
Intent flags is used with Context#startActivity(Intent) via
fun Context.launchActivity(cls: Class<*>, flags: Int) {
val intent = Intent(this, cls).apply {
addFlags(flags)
}
this.startActivity(intent)
}There are many flags, and today we will focus on the flags which have the prefix FLAG_ACTIVITY_.
FLAG_ACTIVITY_NEW_TASK
- Use it with
android:taskAffinityin the AndroidManifest.xml
If the activity doesn’t exist, it will be created at the start of the task (the task taskAffinity defined, otherwise the default task).
<activity
android:name=".activity"
android:taskAffinity=".activity" />- Combine it with other flags.
- Use it with
FLAG_ACTIVITY_CLEAR_TASK
addFlags(FLAG_ACTIVITY_CLEAR_TASK or FLAG_ACTIVITY_NEW_TASK)The current task will be cleared and this activity will be the root of the task.

2. Use it with FLAG_ACTIVITY_MULIPLE_TASK
A new task will be created and the new activity will be the root of the task.

FLAG_ACTIVITY_CLEAR_TASK
This can only be used in conjunction with
FlAG_ACTIVITY_NEW_TASK.
Please refer to FLAG_ACTIVITY_NEW_TASK above.
FLAG_ACTIVITY_CLEAR_TOP
If the activity already exists, all of the activities above it will be destroyed. For itself, will be destroyed and recreated.

- Combine it with other flags.
- Use it with
FLAG_ACTIVITY_SINGLE_TOP
If the activity already exists, all of the activities above it will be destroyed. For itself, will invoke onNewIntent() instead of being recreated.

FLAG_ACTIVITY_SINGLE_TOP
This is the same as android:launchMode="singleTop" when using it alone.
- If the activity exists and on the top of the task, it won’t create a new instance, instead it will call the top activity’s
onNewIntent().

2. If the activity exists but not on the top of the task, another instance will be created and be put on the top of the current task.

FLAG_ACTIVITY_MULTIPLE_TASK
This flag is ignored if one of
de>FLAG_ACTIVITY_NEW_TASK orde>FLAG_ACTIVITY_NEW_DOCUMENT is not also set.
- Use it with
FLAG_ACTIVITY_NEW_TASK
Please refer to FLAG_ACTIVITY_NEW_TASK above.
FLAG_ACTIVITY_NO_HISTORY
If set, the new activity is not kept in the history stack. As soon as the user navigates away from it, the activity is finished. This may also be set with the
de>noHistory attribute.
If set,
de>onActivityResult() is never invoked when the current activity starts a new activity which sets a result and finishes.


FLAG_ACTIVITY_NO_ANIMATION
This flag will prevent an activity transition animation when directing to the next activity.

That’s all for today, I hope this article helps you to clarify all the activity intent flags.
If you have any questions or any suggestions, welcome to leave a comment under this article. 👍 👍





