Search code examples
androidkotlinmaterial-dialog

Material Dialog suddenly closes when going to another activity in Android Kotlin


I have a material dialog inside onForegroundStart() in my BaseActivity. I have extended all my Activities in BaseActivity and opening an app will go to SplashActivity -> HomeActivity if the user is logged in. However, I have noticed that the dialog will suddenly closes when going to another activity. Is there anything that I have missed?

@OnLifecycleEvent(Lifecycle.Event.ON_CREATE)
    fun onCreate() {
        if (isLoggedIn()) {
            dialog = MaterialDialog(this)
                .show {
                    cancelOnTouchOutside(false)
                    message(text = "Session has expired")
                    positiveButton(R.string.ok) {
                        dismiss()
                        userLogout()
                    }
                }
        }
    }

Solution

  • Yes dialogs is paired with activity - if you open dialog in activity and you move this activity to background, dialog will be removed with activity and if you move your activity back to foreground dialog will be showed with activity. So if you show dialog you should wait with opening another activity.