如何制作一个sington对话框 [英] how to make a sington dialog

查看:126
本文介绍了如何制作一个sington对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在android应用中,具有多个片段的一些活动. 即使这些活动或片段不在backStack的顶部,它们也可能仍在运行,并接收来自其他服务的通知.

In android app, having a few activities with multiple fragments. Those activities or fragment could be running alive even if it's not on the top of the backStack, and receiving notifications from different services.

当某些事件发生时,需要显示一个对话框来与用户通信.堆栈顶部的活动或片段可能没有该事件的处理程序.任何其他感兴趣的活动或片段都应做出反应,以向用户打开一个对话框.

When some event happens it is required to show a dialog to communicate with user. The activity or fragment on top of the stack may not have the handler for that event. Any other activity or fragment who is interested should react to open one dialog to the user.

问题在于活动中的侦听器和用于独立处理事件的片段不知道是否已经显示了相同的对话框.

The problem is the listeners in the activities and fragments to handle the events independently don't know if there has been already the same dialog displayed.

告诉处理程序是否应该打开对话框的最佳方法是什么? 由于此对话框对于同一事件是相同的,因此如果可以将其作为单例使用,则可能会有所帮助.

What is best way to tell whether the handler should open the dialog or not? Since this dialog is same for the same event so it may help if could have it as a singleton.

任何人都建议如何使这种情况下的对话框成为单例?

Anyone have suggestion how to make the dialog a singleton for this type of situation?

推荐答案

您无法使Dialog单调,因为Dialog与当前视图链接.而且您当前的视图可能会更改-对话框可能是从活动的不同实例(一个或多个)发出的.

You can't make Dialog singletone, because Dialogs are linked with current view. And your current view may change - dialog may be sown from different instances of the activities (one or many).

但是您可以实现一个简单的单调类来存储所有对话框数据-将数据保存到onPause中并在Dialog的onCreateView中进行检索.

But you can implement a simple singletone class to store all dialog data - save data into onPause and retrieve it in Dialog's onCreateView.

因此,您将获得包含所有数据的单例实例,但是对话框可能会根据当前视图而有所不同.

So you will get singleton instance with all data, but Dialogs may vary according to current view.

您还可以在该单调类中存储指向所显示对话框的链接.使用这种方法,您可以检测到您的对话框是否当前显示.

Also you can store a weak link to the shown dialog in that singletone class. Using such method, you can detect is your dialog currently shown or not.

这篇关于如何制作一个sington对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆