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);
- The System creates a new instance of the MainActivity class.
- This instance is put on the top of the current stack.
- 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 :
So, Let’s check them out one by one:
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);
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.
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);
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.
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);
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.
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);
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.
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.
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.