firebase / FirebaseUI-Android

Optimized UI components for Firebase
https://firebaseopensource.com/projects/firebase/firebaseui-android/
Apache License 2.0
4.61k stars 1.83k forks source link

Firebase Phone Auth UI crash when calling UiModeManager setApplicationNightMode #2061

Closed ArcherEmiya05 closed 1 year ago

ArcherEmiya05 commented 1 year ago

Welcome to FirebaseUI and thanks for submitting an issue!

Please take a look at open issues, as well as resolved issues, to see if your issue is either already being addressed, or has been solved by someone else.

If not, please feel free to fill in the following info so we can help faster!

Step 1: Are you in the right place?

Yes

Step 2: Describe your environment

Step 3: Describe the problem:

We are calling UiModeManager setApplicationNightMode in Application class and the start up Activity launch Firebase Phone Auth intent. According to Android documentation

Changes to night mode take effect locally and will result in a configuration change (and potentially an Activity lifecycle event) being applied to this application.

It could be that by the time when configuration change occur, the launching of Phone Auth UI Activity also happen. In this case what work around can we possibly do?

Steps to reproduce:

  1. Integrate Firebase Phone Auth UI
  2. Add support for day and night theme switching

Observed Results:

Attempting to launch an unregistered ActivityResultLauncher with contract androidx.activity.result.contract.ActivityResultContracts$StartActivityForResult@57b4f96 and input Intent { cmp=com.sample.app/com.firebase.ui.auth.KickoffActivity (has extras) }. You must ensure the ActivityResultLauncher is registered before calling launch().

Note that this only happens on first launch after installing the app

Expected Results:

No crash

Relevant Code:

Inside Application onCreate


val prefModeIsNight = sharedPref.prefBoolean(DARK_THEME)

        // This will be the top level handling of theme
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            uiModeManager.setApplicationNightMode(
                when (prefModeIsNight) {
                    true -> UiModeManager.MODE_NIGHT_YES
                    else -> UiModeManager.MODE_NIGHT_NO
                }
            )
        }
        else {
            AppCompatDelegate.setDefaultNightMode(
                when (prefModeIsNight) {
                    true -> AppCompatDelegate.MODE_NIGHT_YES
                    else -> AppCompatDelegate.MODE_NIGHT_NO
                }
            )
        }

Launching Activity

private val activityResultLauncher = activityForResult { result, _ ->
        // Handle Intent result
    }

... onCreate

if (user == null)
activityResultLauncher.launch(FirebaseUIPhoneAuthUtil.getAuthIntent(R.style.Theme_App_FirebaseAuthUI))
ArcherEmiya05 commented 1 year ago

This issue was solved after moving the initialization of activityResultLauncher inside onCreate since calling UiModeManager setApplicationNightMode will trigger configuration changes which recreates the Activity.