Is there a way to launch an activity such that it displays in a similar way to "launchMode=singleInstance" but still can launch other activities using startActivityForResult?
I want to start an activity in dialog form from a widget but I don't want to bring up the main application if it's running.
Thanks!
I think you're looking for the FLAG_ACTIVITY_NEW_TASK flag.
Just set this flag on your intent and you should get the behavior you want.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
NOTE: It will only create a new task if the activity you're trying to start isn't already running in a task. Otherwise it brings that task to the front.
Related
I'm following this guide to show my activity in Recent Screen like a separate task.
I've added this flag to open my activity in a new task:
newDocumentIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_DOCUMENT);
When I start an activity using startActivityForResut the new activity is not opened as a separate task.
However, if I start an activity using startActivity it is opening in a new task.
Is this is the default behavior? or am I missing something?
See the documentation for FLAG_ACTIVITY_NEW_TASK:
This flag can not be used when the caller is requesting a result from the activity being launched.
So I guess if you're asking for a result, android forces you to stay in the same task.
My app receives pushes and opens different activities according to the push type.
I use TaskStackBuilder for a pending Intent to create a synthetic backstack in conjunction with android:parentActivityNamein my manifest.
So far, so easy. When the App is not started, all works as expected. But if the app is in background (task is running), the pending Intent also starts my desired activity with the defined parent from the manifest, but resets the existing task. The problem is that other activities that were started by the user in the meantime are also cleared.
So what a want to achieve is:
if the app is not started, open the desired activity with the synthetic backstack (MainActivity)
if the app is running, respect the current task order and just push the desired activity on top of it.
I can't seem to make it work with the TaskStackBuilder.
I'd be happy for some insights.
You can't really do this with TaskStackBuilder. It isn't designed for that. It always resets the task to begin with.
I would do the following:
Have the Notification start an Activity. Don't use TaskStackBuilder and don't create any artificial back-stack. This Activity will run in the application's current task if the application is currently active, and it will be put on top of the most recent Activity that is open.
In onCreate() of this new Activity, check if this Activity is the root of the task using isTaskRoot(). If this Activity is the root of the task, this means that the app was not active prior to launching this Activity. In this case, you can create an artificial back-stack using TaskStackBuilder and launch the thing again the way you want it (this will reset the task).
Try using PendingIntent.getActivities with FLAG_ONE_SHOT, this way I was able to open stack of activities with correct navigation
To avoid OnDestory() and OnCreate() beeing called all the time when navigating between my main activity and some sub activities, I have set the singleTop option in my manifest. Unfortunatly this causes that the UI in my main activity is gone when returning from a sub activity. Do I really need to redraw my UI manually?
I am still wondering how to handle these basic navigation features. Is it unusual to perform application initialization tasks (e.g. start services) in OnCreate() of the main activity?
I must be missing something from your explanation I guess because the very basic point that you provided is not correct. When you launch another (sub) activity from your activity then your starting activity is not destroyed but it is stopped. So your onStart is called when you navigate back.
So maybe you can explain a little bit more the background of your requirements.
Use android:launchMode="singleTask" for your main activity so the system will not re-create it if you call it via Intent. You can also clear the stack and go back to your main activity by using
Intent intent = new Intent(SubActivity.this, MainActivity.class);
intentHome.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intentHome.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intentHome);
I would like to launch an activity (say abc) in a different task every time I launch the activity with flag_new_task.
How do i set a different affinity to the activity each time I launch it.
thanks,
If you set both the flags Intent.FLAG_ACTIVITY_MULTIPLE_TASK and Intent.FLAG_ACTIVITY_NEW_TASK when launching the activity, then Android will create a new task every time you do it.
BIG WARNING
HOWEVER, if you create many tasks like this using the same activity, there is no way for the user to return to a specific task by long-pressing on the HOME button. You need to make sure that you don't confuse the user when you do this.
See
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_MULTIPLE_TASK
I have created an activity. I made the launch mode as singleTask in manifest file. I donot want multiple instance of the same activity should start. I am lunching this activity on button click of another activity.
If i click the button more than once then as i have made the activity as single task then If the activity already running then nothing happens. But I want to relaunch the activity without creating another instance. How to achieve this.
Thanks
Deepak
But I want to relaunch the activity without creating another instance
What you meant my relaunch without creating. I think you may have set of code that you have placed in your onCreate and you want to run when the activity again got control. If so its better for you to place that code to your onResume or onNewIntent. Because onCreate of the single task will only execute once
singleTask
The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.