Skip to content

Commit

Permalink
Fix LoginFragment crash on activity recreation
Browse files Browse the repository at this point in the history
App can go in background while LoginFragment is visible, where it can be killed by system.
If VncActivity is the recreated, old VncViewModel would be gone and loginType will be null.
Instead of crashing on null loginType, now the LoginFragment is simply dismissed.
  • Loading branch information
gujjwal00 committed Mar 6, 2024
1 parent 0545440 commit 4354fc2
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions app/src/main/java/com/gaurav/avnc/ui/vnc/LoginFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package com.gaurav.avnc.ui.vnc
import android.app.Dialog
import android.os.Bundle
import android.util.ArrayMap
import android.util.Log
import android.widget.ArrayAdapter
import androidx.core.view.isVisible
import androidx.fragment.app.DialogFragment
Expand Down Expand Up @@ -42,6 +43,14 @@ class LoginFragment : DialogFragment() {
private val loginInfo by lazy { getLoginInfoFromProfile(viewModel.profile) }

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
if (savedInstanceState != null && viewModel.loginInfoRequest.value == null) {
Log.i(javaClass.simpleName, "Activity is being recreated and old ViewModel is gone, removing stale login dialog")
showsDialog = false
dismiss()
return Dialog(requireContext()) // Can't return null
}

check(viewModel.loginInfoRequest.value != null) { "Login fragment invoked without a login type" }
binding = FragmentCredentialBinding.inflate(layoutInflater, null, false)

binding.loginInfo = loginInfo
Expand Down

0 comments on commit 4354fc2

Please sign in to comment.