diff --git a/vector/src/main/java/im/vector/app/core/extensions/Activity.kt b/vector/src/main/java/im/vector/app/core/extensions/Activity.kt index ee94013d1b..02806d81a9 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/Activity.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/Activity.kt @@ -22,36 +22,37 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentTransaction import im.vector.app.core.platform.VectorBaseActivity -fun VectorBaseActivity.addFragment(frameId: Int, fragment: Fragment) { - supportFragmentManager.commitTransaction { add(frameId, fragment) } +fun VectorBaseActivity.addFragment(frameId: Int, fragment: Fragment, allowStateLoss: Boolean = false) { + supportFragmentManager.commitTransaction(allowStateLoss) { add(frameId, fragment) } } -fun VectorBaseActivity.addFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null) { - supportFragmentManager.commitTransaction { +fun VectorBaseActivity.addFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null, allowStateLoss: Boolean = false) { + supportFragmentManager.commitTransaction(allowStateLoss) { add(frameId, fragmentClass, params.toMvRxBundle(), tag) } } -fun VectorBaseActivity.replaceFragment(frameId: Int, fragment: Fragment, tag: String? = null) { - supportFragmentManager.commitTransaction { replace(frameId, fragment, tag) } +fun VectorBaseActivity.replaceFragment(frameId: Int, fragment: Fragment, tag: String? = null, allowStateLoss: Boolean = false) { + supportFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragment, tag) } } -fun VectorBaseActivity.replaceFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null) { - supportFragmentManager.commitTransaction { +fun VectorBaseActivity.replaceFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null, allowStateLoss: Boolean = false) { + supportFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragmentClass, params.toMvRxBundle(), tag) } } -fun VectorBaseActivity.addFragmentToBackstack(frameId: Int, fragment: Fragment, tag: String? = null) { - supportFragmentManager.commitTransaction { replace(frameId, fragment).addToBackStack(tag) } +fun VectorBaseActivity.addFragmentToBackstack(frameId: Int, fragment: Fragment, tag: String? = null, allowStateLoss: Boolean = false) { + supportFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragment).addToBackStack(tag) } } fun VectorBaseActivity.addFragmentToBackstack(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null, + allowStateLoss: Boolean = false, option: ((FragmentTransaction) -> Unit)? = null) { - supportFragmentManager.commitTransaction { + supportFragmentManager.commitTransaction(allowStateLoss) { option?.invoke(this) replace(frameId, fragmentClass, params.toMvRxBundle(), tag).addToBackStack(tag) } diff --git a/vector/src/main/java/im/vector/app/core/extensions/Fragment.kt b/vector/src/main/java/im/vector/app/core/extensions/Fragment.kt index 6ba250ee1b..b3e34ab6a9 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/Fragment.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/Fragment.kt @@ -26,62 +26,62 @@ import java.text.SimpleDateFormat import java.util.Date import java.util.Locale -fun VectorBaseFragment.addFragment(frameId: Int, fragment: Fragment) { - parentFragmentManager.commitTransaction { add(frameId, fragment) } +fun VectorBaseFragment.addFragment(frameId: Int, fragment: Fragment, allowStateLoss: Boolean = false) { + parentFragmentManager.commitTransaction(allowStateLoss) { add(frameId, fragment) } } -fun VectorBaseFragment.addFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null) { - parentFragmentManager.commitTransaction { +fun VectorBaseFragment.addFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null, allowStateLoss: Boolean = false) { + parentFragmentManager.commitTransaction(allowStateLoss) { add(frameId, fragmentClass, params.toMvRxBundle(), tag) } } -fun VectorBaseFragment.replaceFragment(frameId: Int, fragment: Fragment) { - parentFragmentManager.commitTransaction { replace(frameId, fragment) } +fun VectorBaseFragment.replaceFragment(frameId: Int, fragment: Fragment, allowStateLoss: Boolean = false) { + parentFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragment) } } -fun VectorBaseFragment.replaceFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null) { - parentFragmentManager.commitTransaction { +fun VectorBaseFragment.replaceFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null, allowStateLoss: Boolean = false) { + parentFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragmentClass, params.toMvRxBundle(), tag) } } -fun VectorBaseFragment.addFragmentToBackstack(frameId: Int, fragment: Fragment, tag: String? = null) { - parentFragmentManager.commitTransaction { replace(frameId, fragment, tag).addToBackStack(tag) } +fun VectorBaseFragment.addFragmentToBackstack(frameId: Int, fragment: Fragment, tag: String? = null, allowStateLoss: Boolean = false) { + parentFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragment, tag).addToBackStack(tag) } } -fun VectorBaseFragment.addFragmentToBackstack(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null) { - parentFragmentManager.commitTransaction { +fun VectorBaseFragment.addFragmentToBackstack(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null, allowStateLoss: Boolean = false) { + parentFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragmentClass, params.toMvRxBundle(), tag).addToBackStack(tag) } } -fun VectorBaseFragment.addChildFragment(frameId: Int, fragment: Fragment, tag: String? = null) { - childFragmentManager.commitTransaction { add(frameId, fragment, tag) } +fun VectorBaseFragment.addChildFragment(frameId: Int, fragment: Fragment, tag: String? = null, allowStateLoss: Boolean = false) { + childFragmentManager.commitTransaction(allowStateLoss) { add(frameId, fragment, tag) } } -fun VectorBaseFragment.addChildFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null) { - childFragmentManager.commitTransaction { +fun VectorBaseFragment.addChildFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null, allowStateLoss: Boolean = false) { + childFragmentManager.commitTransaction(allowStateLoss) { add(frameId, fragmentClass, params.toMvRxBundle(), tag) } } -fun VectorBaseFragment.replaceChildFragment(frameId: Int, fragment: Fragment, tag: String? = null) { - childFragmentManager.commitTransaction { replace(frameId, fragment, tag) } +fun VectorBaseFragment.replaceChildFragment(frameId: Int, fragment: Fragment, tag: String? = null, allowStateLoss: Boolean = false) { + childFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragment, tag) } } -fun VectorBaseFragment.replaceChildFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null) { - childFragmentManager.commitTransaction { +fun VectorBaseFragment.replaceChildFragment(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null, allowStateLoss: Boolean = false) { + childFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragmentClass, params.toMvRxBundle(), tag) } } -fun VectorBaseFragment.addChildFragmentToBackstack(frameId: Int, fragment: Fragment, tag: String? = null) { - childFragmentManager.commitTransaction { replace(frameId, fragment).addToBackStack(tag) } +fun VectorBaseFragment.addChildFragmentToBackstack(frameId: Int, fragment: Fragment, tag: String? = null, allowStateLoss: Boolean = false) { + childFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragment).addToBackStack(tag) } } -fun VectorBaseFragment.addChildFragmentToBackstack(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null) { - childFragmentManager.commitTransaction { +fun VectorBaseFragment.addChildFragmentToBackstack(frameId: Int, fragmentClass: Class, params: Parcelable? = null, tag: String? = null, allowStateLoss: Boolean = false) { + childFragmentManager.commitTransaction(allowStateLoss) { replace(frameId, fragmentClass, params.toMvRxBundle(), tag).addToBackStack(tag) } } diff --git a/vector/src/main/java/im/vector/app/core/extensions/FragmentManager.kt b/vector/src/main/java/im/vector/app/core/extensions/FragmentManager.kt index 1d437c0701..49b28fb190 100644 --- a/vector/src/main/java/im/vector/app/core/extensions/FragmentManager.kt +++ b/vector/src/main/java/im/vector/app/core/extensions/FragmentManager.kt @@ -27,6 +27,11 @@ inline fun androidx.fragment.app.FragmentManager.commitTransactionNow(func: Frag } } -inline fun androidx.fragment.app.FragmentManager.commitTransaction(func: FragmentTransaction.() -> FragmentTransaction) { - beginTransaction().func().commit() +inline fun androidx.fragment.app.FragmentManager.commitTransaction(allowStateLoss: Boolean = false, func: FragmentTransaction.() -> FragmentTransaction) { + val transaction = beginTransaction().func() + if (allowStateLoss) { + transaction.commitAllowingStateLoss() + } else { + transaction.commit() + } } diff --git a/vector/src/main/java/im/vector/app/features/crypto/keysbackup/restore/KeysBackupRestoreActivity.kt b/vector/src/main/java/im/vector/app/features/crypto/keysbackup/restore/KeysBackupRestoreActivity.kt index bef8788f75..40953cb5f6 100644 --- a/vector/src/main/java/im/vector/app/features/crypto/keysbackup/restore/KeysBackupRestoreActivity.kt +++ b/vector/src/main/java/im/vector/app/features/crypto/keysbackup/restore/KeysBackupRestoreActivity.kt @@ -87,13 +87,13 @@ class KeysBackupRestoreActivity : SimpleFragmentActivity() { viewModel.navigateEvent.observeEvent(this) { uxStateEvent -> when (uxStateEvent) { KeysBackupRestoreSharedViewModel.NAVIGATE_TO_RECOVER_WITH_KEY -> { - addFragmentToBackstack(R.id.container, KeysBackupRestoreFromKeyFragment::class.java) + addFragmentToBackstack(R.id.container, KeysBackupRestoreFromKeyFragment::class.java, allowStateLoss = true) } KeysBackupRestoreSharedViewModel.NAVIGATE_TO_SUCCESS -> { viewModel.keyVersionResult.value?.version?.let { KeysBackupBanner.onRecoverDoneForVersion(this, it) } - replaceFragment(R.id.container, KeysBackupRestoreSuccessFragment::class.java) + replaceFragment(R.id.container, KeysBackupRestoreSuccessFragment::class.java, allowStateLoss = true) } KeysBackupRestoreSharedViewModel.NAVIGATE_TO_4S -> { launch4SActivity() diff --git a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt index 76de2daf54..eb024c4db1 100644 --- a/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt +++ b/vector/src/main/java/im/vector/app/features/home/HomeActivity.kt @@ -121,7 +121,7 @@ class HomeActivity : VectorBaseActivity(), ToolbarConfigurable, UnknownDeviceDet is HomeActivitySharedAction.CloseDrawer -> drawerLayout.closeDrawer(GravityCompat.START) is HomeActivitySharedAction.OpenGroup -> { drawerLayout.closeDrawer(GravityCompat.START) - replaceFragment(R.id.homeDetailFragmentContainer, HomeDetailFragment::class.java) + replaceFragment(R.id.homeDetailFragmentContainer, HomeDetailFragment::class.java, allowStateLoss = true) } }.exhaustive }