Android Intents-Part 2 : Using Intent Flags

In our Blog Post of Android Intents – Part 1, we developed understanding about what are Android Intents. In this Blog, we would be covering one of the most important aspects of Android Intents i.e Intent Flag. In case you have not studied the previous blog post, I would suggest you go through it before starting this one.

First, let’s understand what happens in the background when we start an activity with the following code:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
  1. The System creates a new instance of the MainActivity class.
  2. This instance is put on the top of the current stack.
  3.  Activity(MainActivity) is brought to the foreground.

This intended behavior of the Activity can be modified by specifying the Android Intents flags on the intent instance before passing it to the method startActivity(intent).

Android has many flags defined under Intents which a user can use to modify the behavior of the activity, we would be covering most important and generally used in day-to-day development work namely :

  1. FLAG_ACTIVITY_NO_HISTORY
  2. FLAG_ACTIVITY_NO_ANIMATION
  3. FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
  4. FLAG_ACTIVITY_NEW_TASK
  5. FLAG_ACTIVITY_MULTIPLE_TASK
  6. FLAG_ACTIVITY_CLEAR_TASK
  7. FLAG_ACTIVITY_CLEAR_TOP
  8. FLAG_ACTIVITY_REORDER_TO_FRONT

So, Let’s check them out one by one:

FLAG_ACTIVITY_NO_HISTORY

When used, the instance of the intended activity is not kept in the task’s stack i.e once a user navigates away from this activity the user cannot return to it by pressing the back button.This flag is mostly used when you want to perform a one-time operation such as sudden dispatch to a new activity.

Intent intent = new Intent(SelectFlagActivity.this, SplashActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);

FLAG_ACTIVITY_NO_ANIMATION

This Flag disables the usual animation accompanied by an activity transition.This flag was introduced with Android 2.0. Generally, we make use of this flag when you want to make the transition from Activity A to B then from Activity B to Activity C with  Activity B having no user Interaction.

FLAG_ACTIVITY_NEW_TASK

This flag is used to start an Activity in a new Task.Generally, the calling activity with the intended flag is put in a separate task only if there is no other activity with the same affinity in some other task, else the intended activity is put into the same affinity task.

All the activities in an application have the same affinity to remain in the same task, hence all the activities are placed inside the same task but the affinity of any activity can be customized as per the needs.

You can define it in the manifest file like :

<activity
    android:name=".MainActivity"
    android:taskAffinity="com.sarabblog.intentflag.mainactivity" />
Intent intent = new Intent(SampleActivity.this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

FLAG_ACTIVITY_MULTIPLE_TASK

This Flag is always used with FLAG_ACTIVITY_NEW_TASK. When these two flags are used together the intended activity instance become the first and the only element of that task. As per the coding guidelines, we should not use this flag unless we are implementing our own top-level application launcher because everytime the new instance of the activity is created and stored in a different stack and the default system does not include graphical task management, so unless we provide some way for a user to return back to the activities we have launched earlier.

FLAG_ACTIVITY_CLEAR_TASK

This Flag is used to clear the task where the activity instance would reside in.So when we use this flag to start an activity then the current task of the activity is cleared and this new instance is placed inside that task, hence becoming the root of the task.This particular flag is generally used in conjunction with the FLAG_ACTIVITY_NEW_TASK flag.

Intent intent = new Intent(SampleActivity, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

FLAG_ACTIVITY_CLEAR_TOP

This flag is generally used when we do not want to start a new instance of the already started activity but want to bring this specific instance on the top of the stack i.e to bring the activity to the foreground by removing all other instances stacked over the intended instance.

FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS

When we start an activity and press home button, the target activity is shown in the list of recent launch activities.By using this flag with the activity we can suppress this behavior.Generally, this flag is used for security-related issues where you don’t want your activity screenshot to be taken.

Intent intent = new Intent(this, HomeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
startActivity(intent);

FLAG_ACTIVITY_REORDER_TO_FRONT

We mostly use this flag when we want to bring an activity whose instance already exists in the stack to the foreground, while rest of the activities instances are kept as it is in the stack. Suppose there is an existing stack of activities instance as bundled A-B-C-D-E, now Activity E starts Activity C with the flag FLAG_ACTIVITY_REORDER_TO_FRONT so the new stack becomes A-C-D-E-B.

FLAG_ACTIVITY_SINGLE_TOP

When used, If the instance of the target activity is already at the top of the stack then no new instance is created.The old instance of the targetted activity is brought to the front.

FLAG_ACTIVITY_FORWARD_RESULT

Before covering this flag, lets first discuss how do the startActivityForResult(intent) work? So Suppose Activity A start Activity B and Activity B start Activity C.Now, Activity C perform some operation and needs to send the response back. So ideally we would Start Activity B from Activity A and then start Activity C with startActivityForResult(intent,requestCode) from Activity B. Now once Activity C is done with the operation it will call method setResult(int code) and finishes.Now the callback method OnActivityResult() is called of Activity B.

Now imagine a scenario when the result had to be sent to Activity A from Activity C rather than redirecting to Activity B.This is where FLAG_ACTIVITY_FORWARD_RESULT comes into the picture. So in such a scenario Activity B would call Activity C specifying FLAG_ACTIVITY_FORWARD_RESULT
flag. Then Activity C would define a result which is forwarded back to Activity A without Activity B intervention. The result is forwarded only when Activity C and Activity B finishes. The callback method OnActivityResult() is called on Activity A but not called on Activity B as B only forwards the result.

The important point to note here is that the flag FLAG_ACTIVITY_FORWARD_RESULT can only be used in conjunction with startActivity(intent), not with startActivityForResult(Intent) because an activity using the latter method marks the end of a forwarding chain and should handle the result in its callback method.

81 thoughts on “Android Intents-Part 2 : Using Intent Flags”

  1. Is your company fully virtual yet? Corona has changed the World; use the Engajer platform to improve your marketing, sales, and customer support. Normally thousands of dollars, now reduced down to two plans of $49 / mo or $150 / mo for the next 1,000 signups. Signup here: https://bit.ly/engajer-pricing

  2. Good afternoon, I was just taking a look at your site and filled out your contact form. The “contact us” page on your site sends you these messages via email which is why you’re reading through my message at this moment right? That’s the most important accomplishment with any type of advertising, making people actually READ your advertisement and I did that just now with you! If you have something you would like to blast out to lots of websites via their contact forms in the U.S. or anywhere in the world let me know, I can even target your required niches and my charges are very low. Write an email to: LinaMaurusQE43@gmail.com

  3. Hello, I was just taking a look at your site and filled out your feedback form. The contact page on your site sends you messages like this to your email account which is the reason you are reading my message at this moment correct? That’s the holy grail with any kind of online ad, getting people to actually READ your advertisement and that’s exactly what I just accomplished with you! If you have something you would like to promote to thousands of websites via their contact forms in the US or anywhere in the world let me know, I can even focus on specific niches and my costs are very affordable. Shoot me an email here: danielsamson9146@gmail.com

Leave a Reply

Your email address will not be published. Required fields are marked *