Dialog.getContext() 和创建对话框的 Activity 之间有区别吗?

发布于 2024-12-10 21:58:03 字数 871 浏览 0 评论 0原文

我们的应用程序使用嵌套对话框,并且通过从第一个对话框的 getContext() 方法构建一个对话框,我们已经成功地创建了一个位于另一个对话框之上的对话框。所以:

Activity:

//...
Dialog1 dialog = new Dialog1(this);
dialog.show();
//...

Dialog1:

//...
Dialog1(Context context) {
    super(context);
    //etc.
}

public void onSomeCondition() {
    Dialog2 dialog2 = new Dialog2(getContext());
    dialog2.show();
    //etc.
}

但是,有一种情况我们希望在 Dialog1 仍然可见的情况下直接从 Activity 启动 Dialog2。所以我们把这个方法放在Activity中:

public void onSomeOtherCondition() {
    Dialog2 dialog = new Dialog2(this); //crunch
    dialog.show();
    //etc.
}

窗口管理器根本不喜欢这个。那么 getContext() 实际上与 Activity 中的 ContextWrapper 不完全相同吗?如果是这样的话,这个辅助上下文与主上下文有何不同,并且如果(例如)您将 getContext() 从对话框传递回调用 Activity,是否会产生与持有相同的泄漏风险对其他地方的 Context 引用可以做什么?

如果不是上下文,是什么导致了问题?

Our application uses nested dialogs, and we've been successfully making one dialog that sits on top of another dialog by constructing it from the first dialog's getContext() method. So:

Activity:

//...
Dialog1 dialog = new Dialog1(this);
dialog.show();
//...

Dialog1:

//...
Dialog1(Context context) {
    super(context);
    //etc.
}

public void onSomeCondition() {
    Dialog2 dialog2 = new Dialog2(getContext());
    dialog2.show();
    //etc.
}

However, there is a circumstance where we want to launch Dialog2 directly from the Activity while Dialog1 is still visible. So we put this method in the Activity:

public void onSomeOtherCondition() {
    Dialog2 dialog = new Dialog2(this); //crunch
    dialog.show();
    //etc.
}

The window manager doesn't like this at all. So is it that getContext() is actually NOT exactly the same as the ContextWrapper-ness in the Activity? If so exactly how does this secondary context differ from the primary one, and if (for example) you passed back getContext() from a dialog to a calling Activity, would that create the same leak risk as holding on to a Context reference elsewhere can do?

If it's not the context, what's causing the problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事和酒 2024-12-17 21:58:04

我怀疑当对话框 1 可见时从活动 1 启动对话框 2 的问题是因为对话框 1(而不是活动 1)位于活动堆栈的顶部。我绝不是专家,但我怀疑只有位于 Activity 堆栈顶部的 Activity 才能启动新的 Activity。

I suspect the problem with starting Dialog 2 from Activity 1 when Dialog 1 is visible is because Dialog 1 (not Activity 1) is on the top of the activity stack. I'm by no means an expert, but I suspect that only the Activity at the top of the Activity stack can start new Activities.

明明#如月 2024-12-17 21:58:04

我不完全确定上下文是否不同(看起来是不同的),但我怀疑问题是在尝试从 Activity 启动 Dialog2 之前您没有关闭 Dialog1。 WindowManager 可能会生气,因为您试图在 Activity 之上启动一个对话框,但 Dialog1 已经在那里。

长话短说,我认为您需要:

    public void onSomeOtherCondition() {
         this.dismiss();
         mActivity.onSomeOtherCondition(); //we have a reference to the activity
    }

编辑

我在评论中提出的解决方案是将 Dialog1 的上下文传递给 mActivity.onSomeOtherCondition ,以便您可以使用堆栈顶部的上下文创建 Dialog2 。

I'm not entirely certain if the contexts are different (it would appear they are), but I suspect the problem is that you are not dismissing Dialog1 before attempting to start Dialog2 from your Activity. The WindowManager is probably angry because you are attempting to start a dialog on top of your Activity, but Dialog1 is already there.

Long story short, I think you need:

    public void onSomeOtherCondition() {
         this.dismiss();
         mActivity.onSomeOtherCondition(); //we have a reference to the activity
    }

EDIT

The solution I proposed in the comments is to pass Dialog1's context to mActivity.onSomeOtherCondition so that you can create Dialog2 with the context that is at the top of the stack.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文