Reputation: 69964
I am writing an app with two main activities:
MainActivity is a typical application window, and DialogActivity is an activity that I have styled to appear like a dialog by setting android:theme="@style/Theme.AppCompat.Light.Dialog"
From inside MainActivity I can launch the DialogActivity with the following code.
val intent = Intent(this, DialogActivity::class.java)
startActivity(intent)
The dialog window then appears over the main one like this:
In addition to that, I would also like to be able to launch the dialog by tapping on a notification. If I have a different application open and tap the notification the dialog appears on top of the other app, like I want:
However, if I currently have MainActivity open and then tap the notification the dialog appears on top of a blank background:
This happens despite the fact that the dialog appears "on top" of the main activity window in the overview screen:
So here is my question: If I have MainActivity in the foreground and then launch the DialogActivity by tapping on an appropriate notification, can I have the DialogActivity appear on top of the MainActivity window that was open?
In other words when I tap the notification I want it to look like the first picture if the MainActivity is already open in the foreground, like the second picture if another app is in the foreground, and like the third one only if there are no apps currently open.
I am using the following PendingIntent to launch the activity from the notification. Perhaps there is a is a special set of intent flags that do what I want? Or maybe I need to do something more drastic like merging the two separate activities into a single activity?
val intent = Intent(this, DialogActivity::class.java)
intent.flags = 0
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
The screenshots are from an emulator running Android 9 (Api Level 28)
Upvotes: 0
Views: 217
Reputation: 69964
The key to achieve the result I wanted was to set android:taskAffinity=""
in the app manifest. The full steps are explained in this section of the documentation. It might also be worth reading this related question.
taskAffinity
to the empty string, so the dialog does not cover the old app window in the overview screenIf desired, set excludefromRecents="true"
so the dialog does not appear as a separate entry in the overview. It is effectively dismisse if the user navigates away from it.
<activity
android:name=".DialogActivity"
android:theme="@style/Theme.AppCompat.Light.Dialog"
android:excludeFromRecents="true"
android:taskAffinity="">
</activity>
Pass the NEW_TASK and CLEAR_TASK flags when launching the dialog activity:
val intent = Intent(this, DialogActivity::class.java)
intent.flags =
Intent.FLAG_ACTIVITY_NEW_TASK or
Intent.FLAG_ACTIVITY_CLEAR_TASK
val pendingIntent = PendingIntent.getActivity(this, 0, intent, 0)
Upvotes: 0
Reputation: 174
Instead of moving DialogActivity launch application by passing argument along with intent then handle the argument value. Based on that decide launch dialogactivity or not
Upvotes: 1