0

I'm working to an application that makes some webcalls and I want to add a DialogFragment (with a ProgressBar at the center) that shows when I'm waiting for the call to complete.

Webcalls are extended from AsyncTask with a private library that gives an interface (DialogInterface) to set what to do when the dialog it's shown and hidden. For the DialogFragment I've found some nice code from here:
https://code.luasoftware.com/tutorials/android/android-show-loading-and-prevent-touch/

It works without problems from an Activity class, but the DialogInterface has to be set to the WebService builder which is initialized into the Application class

This is the init sequence in the Application class

WebService.init(
    WebServiceBuilder(this)
        .setBaseUrlTest("http://google.it/")
        .setUrlType(UrlType.TEST)
        .setDialogInterface(object : DialogInterface {
            override fun showDialog(context: Context?) {
                if (context != null)
           
                val busyDialog = BusyDialogFragment.show(supportFragmentManager)
            
            }
            override fun hideDialog() {
                //code that hide the DialogFragment
            }
        })

Obviously in the Application class I cannot get the FragmentManager so this code doesn't works.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Alexsius
  • 1
  • 2
  • you can get FragmentManager object from an FragmentActivity object, should make activity object in your Application class – asdcvf Aug 22 '19 at 04:17

1 Answers1

0

You can register an ActivityLifecycleCallbacks in the Application class to know which activity is currently running, and get the FragmentManager:

class MyApplication : Application() {

    private var currentActivityRef : WeakReference<FragmentActivity?>? = null

    private val supportFragmentManager : FragmentManager? 
        get() = currentActivityRef?.get()?.supportFragmentManager

    override fun onCreate() {
        super.onCreate()

        registerActivityLifecycleCallbacks(object : ActivityLifecycleCallbacks {

            override fun onActivityStarted(activity: Activity?) {
                currentActivityRef = WeakReference(activity as? FragmentActivity)
            }

            override fun onActivityStopped(activity: Activity?) {
                currentActivityRef = null
            }

            override fun onActivityPaused(activity: Activity?) {}

            override fun onActivityResumed(activity: Activity?) {}

            override fun onActivityDestroyed(activity: Activity?) {}

            override fun onActivitySaveInstanceState(activity: Activity?,
                                                     outState: Bundle?) {
            }

            override fun onActivityCreated(activity: Activity?,
                                           savedInstanceState: Bundle?) {
            }
        })
    }
}
Akaki Kapanadze
  • 2,552
  • 2
  • 11
  • 21
  • Sorry but this didn't work, at compile time it was ok but not at runtime. There were other null references. I found out that i can setDialogInterface even from an activity getting the WebService instance. I did so in a parent activity and now it works out. – Alexsius Aug 23 '19 at 07:31