[英]Can we interact with background activity when displaying a dialog over it in android
I want to display a dialog over an activity but still want to interact with background activity to perform something, while the dialog is being displayed. 我想在活动上显示一个对话框,但仍要与后台活动交互以在显示对话框时执行某些操作。 How can this be done?
如何才能做到这一点?
You could start a new Thread
to perform this action. 您可以启动一个新
Thread
来执行此操作。 What I recommend is to implement an AsyncTask
我建议执行一个
AsyncTask
Here's a great tutorial to implement these stuff. 这是实现这些内容的出色教程 。
Use a DialogFragment , which is a fragment that displays a dialog window, floating on top of its activity's window. 使用DialogFragment ,它是显示对话框窗口的片段,浮动在其活动窗口的顶部。 This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state.
该片段包含一个Dialog对象,它根据片段的状态适当显示。 Control of the dialog (deciding when to show, hide, dismiss it) should be done through the API here, not with direct calls on the dialog, so your Activity remains in control.
对话框的控制(决定何时显示,隐藏,关闭它)应该通过此处的API来完成,而不是直接在对话框上进行调用,因此您的Activity仍处于控制之中。
A Dialog
is still part of the same Activity
. Dialog
仍然是同一Activity
一部分。 They just use different windows. 他们只是使用不同的窗口。
Since you haven't posted any code, I'll answer you theoretically that when building you Dialog
, you should keep a reference to the Activity
that holds it. 由于您尚未发布任何代码,因此从理论上讲,我将在构建
Dialog
时回答您,您应该保留对保存它的Activity
的引用。 This way you would still be able to communicate with it. 这样,您仍然可以与其通信。
I've found a pretty neat work around for this problem. 我已经找到解决此问题的巧妙方法。 If you place a transparent view on top of everything you can implement a callback to inject an event into the underlying view, but it doesn't have to be an event it can be any code you want to execute on the background view.
如果将透明视图放在所有内容的顶部,则可以实现回调以将事件注入到基础视图中,但是它不一定是事件,它可以是您要在后台视图上执行的任何代码。 I have an open source library that demonstrates this.
我有一个开放源码库来演示这一点。 If you check out my repo at: https://bitbucket.org/warwick/hgdialrepo you will find that this library comes with a demo application.
如果您在以下位置查看我的存储库: https : //bitbucket.org/warwick/hgdialrepo,您会发现该库随附一个演示应用程序。 Within this demo app you will find a cog demo.
在此演示应用程序中,您将找到一个齿轮演示。 If you examine the code behind the cog demo you will see how this can be achieved.
如果您查看cog演示背后的代码,您将看到如何实现。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.