diff --git a/app/src/main/java/io/nekohasekai/sagernet/ktx/Dialogs.kt b/app/src/main/java/io/nekohasekai/sagernet/ktx/Dialogs.kt index e95a346e0..7204b30e2 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ktx/Dialogs.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ktx/Dialogs.kt @@ -19,6 +19,7 @@ package io.nekohasekai.sagernet.ktx +import android.app.Activity import android.content.Context import androidx.appcompat.app.AlertDialog import androidx.fragment.app.Fragment @@ -32,4 +33,15 @@ fun Context.alert(text: String): AlertDialog { .create() } -fun Fragment.alert(text: String) = requireContext().alert(text) \ No newline at end of file +fun Fragment.alert(text: String) = requireContext().alert(text) + +fun AlertDialog.tryToShow() { + try { + val activity = context as Activity + if (!activity.isFinishing) { + show() + } + } catch (e: Exception) { + Logs.e(e) + } +} diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/AssetsActivity.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/AssetsActivity.kt index e9e005dfd..d354f16bb 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/AssetsActivity.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/AssetsActivity.kt @@ -260,7 +260,7 @@ class AssetsActivity : ThemedActivity() { updateAsset(file, versionFile, localVersion) }.onFailure { onMainDispatcher { - alert(it.readableMessage).show() + alert(it.readableMessage).tryToShow() } } diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/BackupFragment.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/BackupFragment.kt index 15cc86f94..3cfa5b9a2 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/BackupFragment.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/BackupFragment.kt @@ -254,7 +254,7 @@ class BackupFragment : NamedFragment(R.layout.layout_backup) { }.onFailure { Logs.w(it) onMainDispatcher { - alert(it.readableMessage).show() + alert(it.readableMessage).tryToShow() } } diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/ConfigurationFragment.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/ConfigurationFragment.kt index de59ad8e5..cebba46aa 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/ConfigurationFragment.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/ConfigurationFragment.kt @@ -115,7 +115,12 @@ class ConfigurationFragment @JvmOverloads constructor( val securityAdvisory by lazy { DataStore.securityAdvisory } fun getCurrentGroupFragment(): GroupFragment? { - return childFragmentManager.findFragmentByTag("f" + DataStore.selectedGroup) as GroupFragment? + return try { + childFragmentManager.findFragmentByTag("f" + DataStore.selectedGroup) as GroupFragment? + } catch (e: Exception) { + Logs.e(e) + null + } } val updateSelectedCallback = object : ViewPager2.OnPageChangeCallback() { @@ -1737,7 +1742,7 @@ class ConfigurationFragment @JvmOverloads constructor( val msg = Protocols.genFriendlyMsg(err) profileStatus.text = if (msg != err) msg else getString(R.string.unavailable) profileStatus.setOnClickListener { - alert(err).show() + alert(err).tryToShow() } } else { profileStatus.setOnClickListener(null) diff --git a/app/src/main/java/io/nekohasekai/sagernet/ui/GroupFragment.kt b/app/src/main/java/io/nekohasekai/sagernet/ui/GroupFragment.kt index ccd80efd4..2738ed43c 100644 --- a/app/src/main/java/io/nekohasekai/sagernet/ui/GroupFragment.kt +++ b/app/src/main/java/io/nekohasekai/sagernet/ui/GroupFragment.kt @@ -461,17 +461,17 @@ class GroupFragment : ToolbarFragment(R.layout.layout_group), if (subscription != null && subscription.bytesUsed > 0L) { // SIP008 & Open Online Config groupTraffic.isVisible = true groupTraffic.text = if (subscription.bytesRemaining > 0L) { - getString( + app.getString( R.string.subscription_traffic, Formatter.formatFileSize( - context, subscription.bytesUsed + app, subscription.bytesUsed ), Formatter.formatFileSize( - context, subscription.bytesRemaining + app, subscription.bytesRemaining ) ) } else { - getString( + app.getString( R.string.subscription_used, Formatter.formatFileSize( - context, subscription.bytesUsed + app, subscription.bytesUsed ) ) }