I'm using an Activity-2 with android:theme="@android:style/Theme.Translucent.NoTitleBar as a dialog started from the Service with startIntent(). When the app is launched, Activity-1 marked as <action android:name="android.intent.action.MAIN"/> is shown and the Service is being started.

When the user leaves the app by clicking home button (Activity-1 is left on the stack) and Activity-2 is shown by the Service, Activity-1 is visible in the background of the dialog.

Is there a way to launch dialog Activity ignoring other activities on the stack?

有帮助吗?

解决方案

You need to start the Activity-2 as a new task.

When you start the Activity-2 from the service , do this

   Intent intent = new Intent(this,Activity2.class);
   intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   startActivity(intent)

Also, make sure the Activity-1 and Activity-2 has different task Affinity , by default activities in same package will have same 'taskAffinityas its package name. You can set a differnt 'taskAffinity' forActiivty-2` , so that it will be launched in different task.

See here http://developer.android.com/guide/components/tasks-and-back-stack.html The affinity comes into play in two circumstances:

When the intent that launches an activity contains the FLAG_ACTIVITY_NEW_TASK flag. A new activity is, by default, launched into the task of the activity that called startActivity(). It's pushed onto the same back stack as the caller. However, if the intent passed to startActivity() contains the FLAG_ACTIVITY_NEW_TASK flag, the system looks for a different task to house the new activity. Often, it's a new task. However, it doesn't have to be. If there's already an existing task with the same affinity as the new activity, the activity is launched into that task. If not, it begins a new task.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top