问题
I am working on a complex Android app and found a strange problem. The problem occurs when I start an activity to show a dialog.
What have I tried
I tried to search for similar problems ("Android activity blocked by home activity" etc) but could not find any.
Due to the complex nature of my app, I still cannot find the minimum representation of the cause -- The Description section below cannot be used to reproduce the problem. It is just something that I think is closely related to the problem. The real cause could be interactions between different subsystems.
Description
The activity is launched by a service with intent flags = FLAG_ACTIVITY_NEW_TASK | FLAG_ACTIVITY_CLEAR_TOP
, which ensures that it will be the topmost activity at the point when it's launched. In its onCreate
method, the activity set its window flags to be FLAG_SHOW_WHEN_LOCKED | FLAG_FULLSCREEN | FLAG_TURN_SCREEN_ON | FLAG_DISMISS_KEYGUARD
, which ensures that even if the screen is locked, the activity will still be displayed. The dialog is also built and shown in the activity's onCreate
method.
The app is tested on Android phones with screen pattern locks using the following steps:
Start the app. Press home button to return to the home activity. Remove the app from the recent app list.
Turn off and lock the screen by pressing the power button.
Ask (through GCM, to be specific) the service to launch the activity once. Dialog is then successfully shown.
Don't click any button on the dialog. Lock the screen again.
Ask the service to launch the activity once more. This time the dialog is also shown but instantly disappears, leaving the screen pattern lock shown.
Unlock the pattern lock and something strange appears: The dialog is now shown behind the home activity. The dialog doesn't respond to touch event. It's just like a wallpaper.

Android Version seems to matter
The problem seems to be only affecting some of the versions. It occurred on a Galaxy Nexus (Android 4.4.2) but didn't occur on a Galaxy Tab 3 (Android 4.1).
回答1:
Accidentally the problem was solved by removing the line
android:theme="@android:style/Theme.Dialog"
for the activity containing the dialog from AndroidManifest.xml
.
I am not sure why it could be solved in this way though.
来源:https://stackoverflow.com/questions/22166518/dialog-rendered-behind-home-activity