0

I am writing an app with two main activities:

  • MainActiviy
  • DialogActivity

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:

enter image description here

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:

enter image description here

However, if I currently have MainActivity open and then tap the notification the dialog appears on top of a blank background:

enter image description here

This happens despite the fact that the dialog appears "on top" of the main activity window in the overview screen:

enter image description here

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)

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
hugomg
  • 68,213
  • 24
  • 160
  • 246
  • Try to launch `MainActivity` from the notification, instead of the `DialogActivity`, and pass some data to `MainActivity` to indicate that you are launching it from the notification, and if that data is available then launch `DialogActivity` – hiddeneyes02 Oct 26 '19 at 17:58
  • @hiddeneyes02 I still want it to look like the second picture if I tap the notification when another app is open. Is there a way to do that if I use a single activity? – hugomg Oct 26 '19 at 18:06
  • Did you try this: https://stackoverflow.com/a/39691342/3016293 – hiddeneyes02 Oct 26 '19 at 18:36
  • Thanks, that was exactly what I was looking for! – hugomg Oct 26 '19 at 19:06

2 Answers2

1

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

Thirumalai
  • 176
  • 9
  • I don't want to show the MainActivity window unless it is already open in the foreground. I still want it to look like the second picture if another app is currently on the foreground. Is there a way to do this with a single activity? – hugomg Oct 26 '19 at 18:24
0

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.

  1. In the app manifest, set the taskAffinity to the empty string, so the dialog does not cover the old app window in the overview screen
  2. If 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>
    
  3. 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)
    
hugomg
  • 68,213
  • 24
  • 160
  • 246