问题
Hi my app has 3 activities one of which is a dialog. I've done this by using this code in the AndroidManifest.xml
android:theme="@android:style/Theme.Dialog"
the application also has a service which can open the dialog even if the application is not visible, however when it does this it also opens one of the other activities in the background. Is there a way to stop this happening so it opens the dialog in front of the current activity (even if the activity is from a different application)?
Thanks, ng93
回答1:
I solved this by adding:
android:taskAffinity=".MyDialog"
to my dialog in the AndroidManifest.xml. Then it is not related to your other tasks and can be shown without bringing up the suspended activity.
回答2:
You're not able to display something in front of another application's activity. "in front" is somewhat ambiguous, so I'll clarify in that your application can't simultaneously share the screen with another application.
The way I understand it, Android's design inherently requires that when you're doing something on-screen, your app's process/task is in the foreground and in focus, thus bringing the other activities for that task into focus. This implies that other applications are not running with UI focus while your application is in focus.
This means that, for example, when your app launches an activity, it brings your app's stack of activities into focus and puts the new activity (in this case, your dialog box) on top of your app's stack. The result is that if you hit 'back' while displaying the dialog, you'll go back to the activity that was previously displayed. If the stack is cleared of previous activities, 'back' will take you to the application that was previously being displayed on screen.
There are ways of clearing the stack such as: http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
Depending on your scenario you might want to clear the stack, or you might "exit" the app when the user closes the dialog. Additionally, the dialog activity could override onBackPressed so that if the user hits 'back' the activity "exits" your app, returning the user to what they were doing before the dialog.
Further reading for activities and stacks: http://developer.android.com/guide/topics/fundamentals.html#acttask
回答3:
i ended up closing all my apps activities before opening the dialog activity, not ideal but seems like the only way to get the functionality im after
来源:https://stackoverflow.com/questions/4649874/android-dialog-opening-another-view-behind-it