在对话框中使用ApplicationContext是否安全?[英] Is it safe to use the ApplicationContext for a Dialog?

本文是小编为大家收集整理的关于在对话框中使用ApplicationContext是否安全?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我正在运行一个应用程序,该应用程序从Fragment内部生成对话框警报作为网络回调.这要求您通过可用的Context参考;由于我正在使用Fragment s,因此我使用getActivity().由于回调的网络依赖性,getActivity() can 有时返回null .这崩溃了我的代码.

要解决此错误,我希望将ApplicationContext提供给对话框.这是一个单身人士, lifetimes for Lifetime for Lifetime ,所以我知道它不会被撤回.此外,Dialog的内容是关于付款确认的,因此我相信可以在没有与某个Window的严格关系的情况下显示合理的范围.

这是安全的实施吗?我是否避免了不可避免的事情,应该完全转变为更强大的模式?

推荐答案

这是安全的实施吗?

imho,否.

在战术上,我希望您的应用在某些情况下崩溃(例如,您的应用程序在网络I/O完成时处于后台),并且我希望您的对话框不一定会尊重适当的主题.由于您的"对话框"似乎与活动没有绑定,因此应以对话为主题的活动的形式 .这可以解决您的可靠性问题和主题问题,尽管在用户在其他应用程序中,如果您的网络I/O所需的时间比您预期的时间更长.

从战略上讲,无论您应用程序的UI层中发生什么,您似乎都在乎网络I/O的结果.在这种情况下,您的应用程序的UI层不应是直接接收网络I/O结果的UI层,感觉就像您的片段现在正在这样做.使用与UI(IntentService,ThreadPoolExecutor,Bare Thread,ThreadPoolExecutor)分离的东西来管理网络I/O.是否使用事件总线(LocalBroadcastManager,greenrobot的EventBus)告诉UI层网络I/O完成,如果UI层不响应事件(因为它在后台,您只注意对于可见UI时的事件),提高A Notification或做其他细微的事情,让用户知道结果.

其他推荐答案

您必须将活动用作对话框的上下文,因为对话框是一个期望UI上下文的组件.因此,在显示对话框之前,您可以使用 isadded()方法:

if (isAdded()) {
    showDialog();
} 

本文地址:https://www.itbaoku.cn/post/2091054.html

问题描述

I'm running an application that generates a dialog alert from within a Fragment as a network callback. This requires you to pass in an available Context reference; since I'm using Fragments, I use getActivity(). Due to the network-dependent nature of the callback, getActivity() can sometimes return null. This crashes my code.

To work around this error, I was hoping to supply the ApplicationContext to the dialog instead. It is a singleton that persists for the lifetime of the application, so I know it won't be withdrawn. In addition, the Dialog's content is with regards to payment confirmation, so I believe there's reasonable scope for it to be displayed without a strict relationship to a certain Window.

Is this a safe implementation? Am I avoiding the inevitable, and should move to a more robust pattern altogether?

推荐答案

Is this a safe implementation?

IMHO, no.

Tactically, I would expect your app to crash in some situations (e.g., your app is in the background at the time the network I/O completes), and I would expect your dialog to not necessarily honor the appropriate theme. Since it seems like your "dialog" is not tied to an activity, it should be an activity, in the form of a dialog-themed activity. That solves your reliability problem and your theme problem, though there is still the possibility that you display a dialog(-themed activity) while the user is in some other app, if your network I/O takes longer than you anticipate.

Strategically, it seems like you care about the results of your network I/O regardless of what happens in the UI layer of your app. In that case, your UI layer of your app should not be the one directly receiving the results of the network I/O, and it feels like your fragment is doing that right now. Use something that is decoupled from your UI (IntentService, JobService, bare thread, ThreadPoolExecutor) for managing the network I/O. Have it use an event bus (LocalBroadcastManager, greenrobot's EventBus) to tell the UI layer that the network I/O completed, and if the UI layer does not respond to the event (because it is in the background and you only pay attention to events when the UI is visible), raise a Notification or do something else subtle to let the user know about the results.

其他推荐答案

You have to use an Activity as Context for your Dialog, because Dialog is a component, that expects the UI context. So before showing the dialog, you can check if the current fragment is currently added to its activity using the isAdded() method:

if (isAdded()) {
    showDialog();
}