简体   繁体   English

在Android中显示背景活动时,我们可以与背景活动进行互动吗?

[英]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.

相关问题 Android 对话对话框(活动上下文)在后台终止活动时不会关闭 - Android Dialog dialog (activity context) not dismissed when activity is killed in background Android:在后台关闭对话框时,Activity无法获得焦点 - Android: Activity does not receive focus when dialog is dismissed in background Android:如何在不重新缓冲的情况下将播放视频从活动移动到另一个活动或对话框? - Android: How can we move a playing video from an activity to another activity or dialog without re-buffering? 如何在Android 4.4.2中的另一个对话框活动上显示对话框活动? - How to show a dialog activity over another dialog activity in android 4.4.2? Android:在对话框中关闭活动 - Android: Activity closing when in dialog Android:后台的按钮“对话框”的活动处于活动状态? - Android : Buttons in the background Activity of a 'Dialog' is active? 如何与顶部对话框活动下方的活动进行交互? - How to interact with the activity beneath the top dialog activity? 背景模糊,如显示对话框时 - Dim background like when displaying a dialog Android:如何在显示活动之前在后台初始化活动 - Android : how to initialize an activity in background before displaying it 当我们从后台进入前台时,我们如何知道当前正在加载哪个片段? - How can we know that in an activity which fragment is loaded currently when we come in foreground from background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM