M. Schenk
M. Schenk

Reputation: 314

How to enforce activity if app is running?

I start an Activity from my Widget, which should start a special view.
But if the app is already running (not left with back button), Android instead activates the activity that was last shown.
Is there any flag or other way to avoid this behaviour?
Closing the previous activity wouldn't be a problem in my app, there's no user input that would be lost.
I tried a workaround with finish() in onStop(), but onStop is also invoked when a sub activity (startActivityForResult) is invoked, so returning from there returns to nothing - the app would be closed.

Upvotes: 1

Views: 315

Answers (3)

neteinstein
neteinstein

Reputation: 17613

Call intent with FLAG_ACTIVITY_NEW_TASK and on main activity add:

android:launchMode="singleTask" android:clearTaskOnLaunch="true"

On the others add:

android:finishOnTaskLaunch="true"

This way it will kill off any activity when returning to the app after being in background.

Upvotes: 0

Rob
Rob

Reputation: 11

I had this problem too and solved it by using android:launchMode="singleInstance" in each of my activity declaration.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1007584

Try android:clearTaskOnLaunch="true" in the manifest of the activity containing the "special view".

Upvotes: 0

Related Questions