avatarEvan Fang

Summary

This text discusses the use of Intent flags of activity with startActivity() in Android development, focusing on the flags that have the prefix FLAG_ACTIVITY_.

Abstract

The text provides an in-depth explanation of various Intent flags used in Android development with startActivity(). These flags, which have the prefix FLAG_ACTIVITY_, are used with Context#startActivity(Intent) via setFlags(int) and addFlags(int). The article covers flags such as FLAG_ACTIVITY_NEW_TASK, FLAG_ACTIVITY_CLEAR_TASK, FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_SINGLE_TOP, FLAG_ACTIVITY_MULTIPLE_TASK, FLAG_ACTIVITY_NO_HISTORY, and FLAG_ACTIVITY_NO_ANIMATION. The author explains how each flag affects the activity's behavior in the task and back stack, and provides examples and visual representations to illustrate the concepts.

Bullet points

  • The text discusses Intent flags of activity used with startActivity() in Android development.
  • The focus is on flags with the prefix FLAG_ACTIVITY_.
  • The flags are used with Context#startActivity(Intent) via setFlags(int) and addFlags(int).
  • The article covers various flags, including:
    • FLAG_ACTIVITY_NEW_TASK
    • FLAG_ACTIVITY_CLEAR_TASK
    • FLAG_ACTIVITY_CLEAR_TOP
    • FLAG_ACTIVITY_SINGLE_TOP
    • FLAG_ACTIVITY_MULTIPLE_TASK
    • FLAG_ACTIVITY_NO_HISTORY
    • FLAG_ACTIVITY_NO_ANIMATION
  • Each flag's effect on the activity's behavior in the task and back stack is explained.
  • Examples and visual representations are provided to illustrate the concepts.

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.

TasksAndBackStackSample

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 de>setFlags(int) and de>addFlags(int) like the code snippet below:

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:taskAffinity in 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.
  1. 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.

The clear top activity recreates. It’s hash changes from 85779657 -> 240436298
  • Combine it with other flags.
  1. 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.

  1. 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’sonNewIntent().

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 or de>FLAG_ACTIVITY_NEW_DOCUMENT is not also set.

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

Press back key won’t direct users back to the no history activity because it will be destroyed automatically.
The no history activity will be destroyed automatically.

FLAG_ACTIVITY_NO_ANIMATION

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

It won’t show the activity transition animation.

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

Android App Development
Intent Flag
Tasks And Backstack
Activity
Recommended from ReadMedium