[SOLVED] Display activity as a popup window – Stack Overflow

Issue

This Content is from Stack Overflow. Question asked by Bialy

I have a service that starts an activity as a dialog/popup window from my app. It works generally ok.

When my app is closed (not in recents), the popup will overlay any underlying app with a transparent surrounding background.

But the issue comes when my app was minimized in the background and I’m using another app, then I click on the service button to display the popup, it brings my app back into the front with the popup over my app (not the previous third-party app).

How can I prevent this behavior and make my activity overly any window regardless of my app’s state?

This is the theme I’m using

<style name="PopupTheme" parent="Theme.AppCompat.Dialog">
</style>



Solution

If the popup dialog has nothing to do with your app (and you want it to be separate and distinct from your app in case your app is already running), then you want to do the following:

In the manifest, set the taskAffinity for this dialog-themed Activity to an empty string, set the excludeFromRecents flag and the noHistory flag like this:

<activity android:name=".DialogThemedActivity"
          android:taskAffinity=""
          android:excludeFromRecents="true"
          android:noHistory="true"
          ... />

Declaring the Activity like this ensures that it is not associated with any existing task that your app is currently in. It also ensures that this task will not end up in the list of recent tasks (so that the user doesn’t accidentally start the Activity again from the list of recent tasks) and that the task will be deleted as soon as the user navigates away from the dialog-themed Activity.

Now, in your Service, make sure that when you launch the Activity you add FLAG_ACTIVITY_NEW_TASK:

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

This is the correct way to do this.


This Question was asked in StackOverflow by Bialy and Answered by David Wasser It is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?