Can we interact with background activity when displaying a dialog over it in android

后端 未结 4 1599
深忆病人
深忆病人 2021-01-21 08:12

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?

相关标签:
4条回答
  • 2021-01-21 08:55

    Use a DialogFragment, which is a fragment that displays a dialog window, floating on top of its activity's window. This fragment contains a Dialog object, which it displays as appropriate based on the fragment's state. 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.

    0 讨论(0)
  • 2021-01-21 08:58

    A Dialog is still part of the same 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. This way you would still be able to communicate with it.

    0 讨论(0)
  • 2021-01-21 09:01

    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. 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.

    0 讨论(0)
  • 2021-01-21 09:11

    You could start a new Thread to perform this action. What I recommend is to implement an AsyncTask

    Here's a great tutorial to implement these stuff.

    0 讨论(0)
提交回复
热议问题