1

Short story: My dialog is hidden when a specific activity is displayed. When other activities are showing, the dialog can be seen fine.

Long story: I have an activity which uses the following flags:

getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED | 
  WindowManager.LayoutParams.FLAG_IGNORE_CHEEK_PRESSES);

It also uses the PROXIMITY_SCREEN_OFF_WAKE_LOCK in order to turn off the screen when you put the phone against your face.

This activity is defined in the manifest as follows:

<activity
    android:name=".ui.MyHidingActivity"
    android:configChanges="orientation|screenSize"
    android:launchMode="singleInstance"
    android:windowSoftInputMode="stateHidden"></activity>

Now, when an event happens in the application I want to create and show this dialog:

KeyguardManager km = (KeyguardManager) getSystemService(KEYGUARD_SERVICE);
final KeyguardManager.KeyguardLock kl = km.newKeyguardLock("com.mycompany.myproject");
kl.disableKeyguard();

PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK, "My_App");
wl.acquire();

final AlertDialog alertDialog = new AlertDialog.Builder(tabActivity).create();
            alertDialog.getWindow().addFlags(
    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
  | WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD
  | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
  | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
alertDialog.setTitle("Incoming Event");
alertDialog.setMessage("You have an incoming event.");
alertDialog.show();
wl.release();

The dialog displays fine when any other activity is displayed, except for that MyHidingActivity which hides the dialog for whatever reason. Any idea what may be the problem?

Emmanuel
  • 16,791
  • 6
  • 48
  • 74
  • Maybe cause is android:launchMode="singleInstance". Launched new task after "MyHidingActivity" was opened. Try start dialog after MyHidingActivity opening or remove android:launchMode="singleInstance". – vmtrue Jun 20 '17 at 04:49
  • for showing the dialog in the MyHidingActivity from which method are you invoking this dialog. – Rameshbabu Jun 20 '17 at 05:27
  • @Rameshbabu From a listener in my Application. The problem is the dialog is attached to a different activity than the one that is displayed. I will post an answer to my own question soon which will explain further. – Emmanuel Jun 20 '17 at 17:39

1 Answers1

3

OK I have found the solution.

The issue was in new AlertDialog.Builder(tabActivity).create() where I pass in the main activity. This doesn't just use this activity as any old Context. The dialog is shown on top of that activity. But the MyHidingActivity is not the same activity as the main one so it hides both the main activity and the dialog it is attached to.

In order to fix this issue, you have to display the dialog on top of all other activities. And for that you have to:

  1. Use the application as a context instead: new AlertDialog.Builder(this).create() (where "this" is my application)
  2. Make the dialog a System dialog:

    alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);

Emmanuel
  • 16,791
  • 6
  • 48
  • 74
  • Of course if you decide to use a system alert, you have to ask permission with android.permission.SYSTEM_ALERT_WINDOW and Settings.ACTION_MANAGE_OVERLAY_PERMISSION. See https://stackoverflow.com/a/35716156/442512 – Emmanuel Sep 08 '17 at 16:37