Home > database >  How to display dialog on any screen (fragment or activity) in android
How to display dialog on any screen (fragment or activity) in android

Time:07-11

User is in Fragment/Activity "A"
In the onCreate() of screen "A" i fire some network call on a background thread. User navigates to some other Fragment/Activity, now when i get the response from the network i need to show the dialog no matter wherever the user is in the app.



How can i achieve this behavior? using DialogFragment

CodePudding user response:

Create a transparent activity and in its onCreate method show your dialog. Set an OnDismissListener for the dialog and in this callback finish the activity. Every where you need showing your dialog you can start this transparent activity

CodePudding user response:

If you want to show FragmentA as dialog on background operation response change fragments supper class to FragmentDialog. This works only if you use Fragment not Activity. And show it by this code:

fun showResponse(fragmentManager:FragmentManager) {
    val responseFragment = FragmentA()//or name of your Fragment
    responseFragment.show(fragmentManager, "")
}

If you want to show dialog from activity or do not show FragmentA, create class ResponseDialog with super class DialogFragment. In this class in method onCreateDialog you can design your dialog (add text, buttons, images...).

class ResponseDialog: DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle): Dialog {
        return activity?.let {
            val builder = AlertDialog.Builder(it)
            builder.setMessage("data downloaded")
                    .setPositiveButton("Ok",
                            DialogInterface.OnClickListener { dialog, id ->
                                //positive button action here dissmis dialog
                            })

         
            builder.create()
        } ?: throw IllegalStateException("Activity cannot be null")
    }
}

And show it by this method:

fun showResponse(fragmentManager:FragmentManager) {
    val responseFragment = ResponseDialog()
    responseFragment.show(fragmentManager, "")
}

Android dialogs documentation

CodePudding user response:

It depends on how complicated you want to do it. A "good approach" is to have some BaseFragment, BaseActivity, BaseViewModel(BasePresenter) in your application. I say "good approach", because you might know the concept of "extend is evil" and "favor composition over inheritance", so you should watch out for not making the base classes too heavy.

But you can put some error handling(notification) logic in the base classes. For example, you can have some "singleton" or something with a lifecycle equal to the screens you navigate through. There you put the Network logic.

You observe the result from different VMs as part of different Activities. Just plain Observer pattern and you might return a boolean result "handled" from the observers so you show the dialog in only one Activity.

When you receive the result in the VM - call(expose some stream of data,etc) the BaseActivity to show the dialog.

  • Related