Prevent DialogFragment.show crash

This commit is contained in:
Mygod
2020-01-23 09:29:51 +08:00
parent 09238462d1
commit 2298f7858f
4 changed files with 13 additions and 4 deletions

View File

@@ -8,6 +8,7 @@ import android.os.Parcelable
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.Fragment
import be.mygod.vpnhotspot.util.showAllowingStateLoss
import kotlinx.android.parcel.Parcelize
/**
@@ -42,7 +43,7 @@ abstract class AlertDialogFragment<Arg : Parcelable, Ret : Parcelable> :
fun show(target: Fragment, requestCode: Int = 0, tag: String = javaClass.simpleName) {
setTargetFragment(target, requestCode)
show(target.parentFragmentManager, tag)
showAllowingStateLoss(target.parentFragmentManager, tag)
}
}

View File

@@ -20,6 +20,7 @@ import be.mygod.vpnhotspot.preference.SharedPreferenceDataStore
import be.mygod.vpnhotspot.preference.SummaryFallbackProvider
import be.mygod.vpnhotspot.util.RootSession
import be.mygod.vpnhotspot.util.launchUrl
import be.mygod.vpnhotspot.util.showAllowingStateLoss
import be.mygod.vpnhotspot.widget.SmartSnackbar
import com.google.android.material.snackbar.Snackbar
import kotlinx.coroutines.Dispatchers
@@ -167,7 +168,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
true
}
findPreference<Preference>("misc.donate")!!.setOnPreferenceClickListener {
EBegFragment().show(parentFragmentManager, "EBegFragment")
EBegFragment().showAllowingStateLoss(parentFragmentManager, "EBegFragment")
true
}
}
@@ -180,7 +181,7 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
app.connectivity.getLinkProperties(it)?.interfaceName
}.toTypedArray())
setTargetFragment(this@SettingsPreferenceFragment, 0)
}.show(parentFragmentManager, preference.key)
}.showAllowingStateLoss(parentFragmentManager, preference.key)
else -> super.onDisplayPreferenceDialog(preference)
}
}

View File

@@ -23,6 +23,7 @@ import be.mygod.vpnhotspot.R
import be.mygod.vpnhotspot.RepeaterService
import be.mygod.vpnhotspot.databinding.DialogWifiApBinding
import be.mygod.vpnhotspot.util.QRCodeDialog
import be.mygod.vpnhotspot.util.showAllowingStateLoss
import be.mygod.vpnhotspot.util.toByteArray
import be.mygod.vpnhotspot.util.toParcelable
import be.mygod.vpnhotspot.widget.SmartSnackbar
@@ -199,7 +200,7 @@ class WifiApDialogFragment : AlertDialogFragment<WifiApDialogFragment.Arg, WifiA
SmartSnackbar.make(e).show()
return false
}
QRCodeDialog().withArg(qrString).show(parentFragmentManager, "QRCodeDialog")
QRCodeDialog().withArg(qrString).showAllowingStateLoss(parentFragmentManager, "QRCodeDialog")
true
}
else -> false

View File

@@ -16,6 +16,8 @@ import androidx.annotation.DrawableRes
import androidx.core.net.toUri
import androidx.core.view.isVisible
import androidx.databinding.BindingAdapter
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import be.mygod.vpnhotspot.App.Companion.app
import be.mygod.vpnhotspot.room.macToString
import be.mygod.vpnhotspot.widget.SmartSnackbar
@@ -60,6 +62,10 @@ fun <T : Parcelable> ByteArray.toParcelable() = useParcel { p ->
p.readParcelable<T>(javaClass.classLoader)
}
fun DialogFragment.showAllowingStateLoss(manager: FragmentManager, tag: String? = null) {
if (!manager.isStateSaved) show(manager, tag)
}
fun broadcastReceiver(receiver: (Context, Intent) -> Unit) = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) = receiver(context, intent)
}