Commit 096a8ba0 authored by Mygod's avatar Mygod

Prevent DialogFragment.show crash

parent 7f8ff169
...@@ -31,6 +31,7 @@ import com.github.shadowsocks.database.Profile ...@@ -31,6 +31,7 @@ import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager import com.github.shadowsocks.database.ProfileManager
import com.github.shadowsocks.plugin.AlertDialogFragment import com.github.shadowsocks.plugin.AlertDialogFragment
import com.github.shadowsocks.plugin.Empty import com.github.shadowsocks.plugin.Empty
import com.github.shadowsocks.plugin.showAllowingStateLoss
import kotlinx.android.parcel.Parcelize import kotlinx.android.parcel.Parcelize
class UrlImportActivity : AppCompatActivity() { class UrlImportActivity : AppCompatActivity() {
...@@ -61,7 +62,7 @@ class UrlImportActivity : AppCompatActivity() { ...@@ -61,7 +62,7 @@ class UrlImportActivity : AppCompatActivity() {
Toast.makeText(this, R.string.profile_invalid_input, Toast.LENGTH_SHORT).show() Toast.makeText(this, R.string.profile_invalid_input, Toast.LENGTH_SHORT).show()
finish() finish()
} }
else -> dialog.show(supportFragmentManager, null) else -> dialog.showAllowingStateLoss(supportFragmentManager)
} }
} }
......
...@@ -34,6 +34,7 @@ import com.github.shadowsocks.preference.DataStore ...@@ -34,6 +34,7 @@ import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.DirectBoot import com.github.shadowsocks.utils.DirectBoot
import com.github.shadowsocks.utils.Key import com.github.shadowsocks.utils.Key
import com.github.shadowsocks.net.TcpFastOpen import com.github.shadowsocks.net.TcpFastOpen
import com.github.shadowsocks.plugin.showAllowingStateLoss
import com.github.shadowsocks.preference.BrowsableEditTextPreferenceDialogFragment import com.github.shadowsocks.preference.BrowsableEditTextPreferenceDialogFragment
import com.github.shadowsocks.preference.EditTextPreferenceModifiers import com.github.shadowsocks.preference.EditTextPreferenceModifiers
import com.github.shadowsocks.preference.HostsSummaryProvider import com.github.shadowsocks.preference.HostsSummaryProvider
...@@ -126,7 +127,7 @@ class GlobalSettingsPreferenceFragment : PreferenceFragmentCompat() { ...@@ -126,7 +127,7 @@ class GlobalSettingsPreferenceFragment : PreferenceFragmentCompat() {
if (preference == hosts) BrowsableEditTextPreferenceDialogFragment().apply { if (preference == hosts) BrowsableEditTextPreferenceDialogFragment().apply {
setKey(hosts.key) setKey(hosts.key)
setTargetFragment(this@GlobalSettingsPreferenceFragment, REQUEST_BROWSE) setTargetFragment(this@GlobalSettingsPreferenceFragment, REQUEST_BROWSE)
}.show(parentFragmentManager, hosts.key) else super.onDisplayPreferenceDialog(preference) }.showAllowingStateLoss(parentFragmentManager, hosts.key) else super.onDisplayPreferenceDialog(preference)
} }
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
......
...@@ -155,7 +155,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(), ...@@ -155,7 +155,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
PluginConfigurationDialogFragment().apply { PluginConfigurationDialogFragment().apply {
setArg(Key.pluginConfigure, pluginConfiguration.selected) setArg(Key.pluginConfigure, pluginConfiguration.selected)
setTargetFragment(this@ProfileConfigFragment, 0) setTargetFragment(this@ProfileConfigFragment, 0)
}.show(parentFragmentManager, Key.pluginConfigure) }.showAllowingStateLoss(parentFragmentManager, Key.pluginConfigure)
} }
private fun saveAndExit() { private fun saveAndExit() {
...@@ -206,7 +206,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(), ...@@ -206,7 +206,7 @@ class ProfileConfigFragment : PreferenceFragmentCompat(),
Key.plugin -> BottomSheetPreferenceDialogFragment().apply { Key.plugin -> BottomSheetPreferenceDialogFragment().apply {
setArg(Key.plugin) setArg(Key.plugin)
setTargetFragment(this@ProfileConfigFragment, 0) setTargetFragment(this@ProfileConfigFragment, 0)
}.show(parentFragmentManager, Key.plugin) }.showAllowingStateLoss(parentFragmentManager, Key.plugin)
Key.pluginConfigure -> { Key.pluginConfigure -> {
val intent = PluginManager.buildIntent(pluginConfiguration.selected, PluginContract.ACTION_CONFIGURE) val intent = PluginManager.buildIntent(pluginConfiguration.selected, PluginContract.ACTION_CONFIGURE)
if (intent.resolveActivity(requireContext().packageManager) == null) showPluginEditor() else { if (intent.resolveActivity(requireContext().packageManager) == null) showPluginEditor() else {
......
...@@ -50,6 +50,7 @@ import com.github.shadowsocks.bg.BaseService ...@@ -50,6 +50,7 @@ import com.github.shadowsocks.bg.BaseService
import com.github.shadowsocks.database.Profile import com.github.shadowsocks.database.Profile
import com.github.shadowsocks.database.ProfileManager import com.github.shadowsocks.database.ProfileManager
import com.github.shadowsocks.plugin.PluginConfiguration import com.github.shadowsocks.plugin.PluginConfiguration
import com.github.shadowsocks.plugin.showAllowingStateLoss
import com.github.shadowsocks.preference.DataStore import com.github.shadowsocks.preference.DataStore
import com.github.shadowsocks.utils.Action import com.github.shadowsocks.utils.Action
import com.github.shadowsocks.utils.datas import com.github.shadowsocks.utils.datas
...@@ -331,9 +332,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener { ...@@ -331,9 +332,7 @@ class ProfilesFragment : ToolbarFragment(), Toolbar.OnMenuItemClickListener {
override fun onMenuItemClick(item: MenuItem): Boolean = when (item.itemId) { override fun onMenuItemClick(item: MenuItem): Boolean = when (item.itemId) {
R.id.action_qr_code -> { R.id.action_qr_code -> {
if (!parentFragmentManager.isStateSaved) { QRCodeDialog(this.item.toString()).showAllowingStateLoss(parentFragmentManager)
QRCodeDialog(this.item.toString()).show(parentFragmentManager, "")
}
true true
} }
R.id.action_export_clipboard -> { R.id.action_export_clipboard -> {
......
...@@ -28,7 +28,6 @@ import android.os.Parcelable ...@@ -28,7 +28,6 @@ import android.os.Parcelable
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDialogFragment import androidx.appcompat.app.AppCompatDialogFragment
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import kotlinx.android.parcel.Parcelize
/** /**
* Based on: https://android.googlesource.com/platform/packages/apps/ExactCalculator/+/8c43f06/src/com/android/calculator2/AlertDialogFragment.java * Based on: https://android.googlesource.com/platform/packages/apps/ExactCalculator/+/8c43f06/src/com/android/calculator2/AlertDialogFragment.java
...@@ -62,9 +61,6 @@ abstract class AlertDialogFragment<Arg : Parcelable, Ret : Parcelable> : ...@@ -62,9 +61,6 @@ abstract class AlertDialogFragment<Arg : Parcelable, Ret : Parcelable> :
fun show(target: Fragment, requestCode: Int = 0, tag: String = javaClass.simpleName) { fun show(target: Fragment, requestCode: Int = 0, tag: String = javaClass.simpleName) {
setTargetFragment(target, requestCode) setTargetFragment(target, requestCode)
show(target.fragmentManager ?: return, tag) showAllowingStateLoss(target.fragmentManager ?: return, tag)
} }
} }
@Parcelize
class Empty : Parcelable
/*******************************************************************************
* *
* Copyright (C) 2020 by Max Lv <max.c.lv@gmail.com> *
* Copyright (C) 2020 by Mygod Studio <contact-shadowsocks-android@mygod.be> *
* *
* This program is free software: you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program. If not, see <http://www.gnu.org/licenses/>. *
* *
*******************************************************************************/
@file:JvmName("Utils")
package com.github.shadowsocks.plugin
import android.os.Parcelable
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.FragmentManager
import kotlinx.android.parcel.Parcelize
@Parcelize
class Empty : Parcelable
@JvmOverloads
fun DialogFragment.showAllowingStateLoss(fragmentManager: FragmentManager, tag: String? = null) {
if (!fragmentManager.isStateSaved) show(fragmentManager, tag)
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment