Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use Intent.FLAG_ACTIVITY_MULTIPLE_TASK?

Background

Let's take the next sceneraio:

  1. the user uses an app X which has multiple activities (like the gmail app).
  2. after navigating the app X a bit, he goes to your app.
  3. in your app, you need to start this X app using an intent, to go to a specific activity within it.
  4. now the user goes to this specific activity on the app X.
  5. the user presses the back button, hoping to go back to your app instead of staying on the app X.

another similar scenario:

  1. the user navigates on your app between activities.
  2. your app went to the background (using the home button, for example).
  3. your app shows a notification that once clicked, it will open a specific activity of your app.
  4. the user clicks on the notification and goes to the specific activity of your app.
  5. the user presses the back button, hoping to return to the app that was shown before clicking on the notification, instead of going to the previous activity on your app that was shown the last time it left it.

It seems that the last step on both scenarios isn't the default behavior.

This is why I've searched what is the best combination of flags for this purpose.

The problem

It seems that the only flag that can achieve the behavior i've described is Intent.FLAG_ACTIVITY_MULTIPLE_TASK (together with Intent.FLAG_ACTIVITY_NEW_TASK).

According to the android API, however, this flag isn't recommended for normal use:

Do not use this flag unless you are implementing your own top-level application launcher. ...

Because the default system does not include graphical task management, you should not use this flag unless you provide some way for a user to return back to the tasks you have launched.

This information seems as confusing as the rest of the descriptions about intents.

There are other intents flags that I've seen, like Intent.FLAG_ACTIVITY_CLEAR_TASK that achieve a similar result, but they have weird behaviors and/or they use high API

The question

Is it safe to use this flag? Are there any good alternatives to it?

What is the danger of using this flag and what is the meaning of the description on the API of using it?

like image 971
android developer Avatar asked Nov 04 '13 15:11

android developer


People also ask

What is intent Flag_activity_new_task?

The flags you can use to modify the default behavior are: FLAG_ACTIVITY_NEW_TASK. Start the activity in a new task. If a task is already running for the activity you are now starting, that task is brought to the foreground with its last state restored and the activity receives the new intent in onNewIntent() .

What is true about flag activity new task?

flag — FLAG_ACTIVITY_CLEAR_TOP: If the Activity being started is already running in the current task then instead of launching the new instance of that Activity, all the other activities on top of it is destroyed (with call to onDestroy method) and this intent is delivered to the resumed instance of the Activity (now ...

What is Flag_activity_clear_top?

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.

What are flags in Android?

A flag is a setting that allows the configuration of or gating of some logic or feature. It can almost be thought of as a simple constant but with some extra functionality that we will go over in this document.


1 Answers

In your first scenario, if your app needs to start an activity of another app, it can just start this activity within the same task as your application. There is no reason to use any special Intent flags for this (you don't need FLAG_ACTIVITY_NEW_TASK or FLAG_ACTIVITY_MULTIPLE_TASK). In step 5 of your scenario, the user will return to your application (because BACK just takes him to the previous activity in the current task). This is the standard, default behaviour.

In your second scenario, my first response is "that isn't the standard behaviour". That means that users probably won't expect to be able to go back to the task that they were doing prior to clicking on the notification. However, if you really want to implement this, then I would suggest that you create a special Activity that is launched from the notification and this special Activity should have a different taskAffinity than the rest of the Activities in your application. In this case, when the special Activity is launched from the notification, it will not bring your application's task to the foreground. It will just create a new task containing just the special Activity. When the special Activity is showing, the user can press the BACK key and this will return him to the task that he was working on prior to clicking on your notification.

In general you should NOT use FLAG_ACTIVITY_MULTIPLE_TASK. The main reason is that if you have several tasks containing your application (or parts of it), it is pretty much impossible for the user to return to a specific one. There is no way to provide different launch icons or different application names (for the different tasks)(, so that the user will see multiple tasks in the "recent tasks" list, but will not be able to tell which one is which. You will have a hard time cleaning up what you are doing and you will just make more problems than you can deal with. There are about a million side effects of using this flag and for general applicaitons there is just no need to do it.

like image 80
David Wasser Avatar answered Sep 27 '22 20:09

David Wasser