3

这就是场景。我的应用程序中有 2 个活动,当我单击应用程序创建的通知时,会启动一个 Dialog 活动。问题是,当我单击通知时,如果在按下主页按钮时停止,则应该只显示对话框,而不是我的应用程序的其他活动。

当我通过按后退按钮关闭应用程序时,对话框活动会显示对话框,但是当应用程序在后台运行时,该活动也会在创建对话框活动时打开。

@android:style/Theme.Dialog用于我的对话活动。

如何只显示 Dialog 活动,而不显示背景中的其他活动?

4

3 回答 3

2

OP 问题的解决方案是在清单中为该活动设置不同的任务关联。

<activity android:name="MyIndependentActivity"
android:theme="@android:style/Theme.Dialog" 
android:taskAffinity="a_unique_id">

taskAffinity 字符串必须不同于包名(com.whatever.myapp)

我还使用 android:excludeFromRecents="true",从最近的应用程序列表中隐藏对话框,因为返回对话框通常没有意义。

更多信息:http: //developer.android.com/guide/components/tasks-and-back-stack.html#Affinities

于 2013-04-07T13:20:15.990 回答
0

是的,那会发生。

当您声明 Dialog 的主题时,它会影响 Activity 生命周期,并且之前的 Activity 不会进入 onStop,因此某些 Android 功能仍然认为它是活跃的 Activity,这在技术上是正确的,因为您的对话框 Activity 就像一个对话框一样。

如果您不“关心”可以看到对话框后面的先前活动,一种可能的解决方法是将对话框更改为 DialogFragment,将对话框的主题放在片段上并将其显示在它自己的活动中,即会做的。

于 2012-07-27T09:47:38.040 回答
0

The way I have done this (and this can't be the best way to do this) is to have some logic in the activity.onPause() and activity.onResume() methods, that will perform actions based on what I want. My experience is more around separate activities and transitioning between them than using dialogs alot.

You can pass information between activities through setResult(). This will enable you to work out why the child activity has decided to close. That combined with the onResume function should enable you to disable the parent activity.

To override the dialog so that the other activity is not visible behind it, is probably to use the onPause() method to make it go translucent.

I have found onStop() very irratic to use and often unnecessary. The reason for this, is that it is called unpredictably from a developers point of view because onStop can be called based at strange times based on whether the OS has enough memory etc. onPause however in my experience is always called predicably.

于 2012-07-27T09:53:43.103 回答