From 2b705c7256a0fd03c67b988f9e678f39d80c45d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Escandell?= Date: Tue, 24 May 2016 08:45:40 +0200 Subject: [PATCH] Prevent Fragment Added Exception --- .../MvxCachingFragmentActivity.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/MvvmCross.Droid.Support.V4/MvxCachingFragmentActivity.cs b/MvvmCross.Droid.Support.V4/MvxCachingFragmentActivity.cs index 334198d..9eecf16 100644 --- a/MvvmCross.Droid.Support.V4/MvxCachingFragmentActivity.cs +++ b/MvvmCross.Droid.Support.V4/MvxCachingFragmentActivity.cs @@ -32,6 +32,7 @@ public class MvxCachingFragmentActivity : MvxFragmentActivity, IFragmentCacheabl public const string ViewModelRequestBundleKey = "__mvxViewModelRequest"; private const string SavedFragmentTypesKey = "__mvxSavedFragmentTypes"; private IFragmentCacheConfiguration _fragmentCacheConfiguration; + private string _firstFragmentTag; protected enum FragmentReplaceMode { @@ -241,6 +242,17 @@ protected virtual void ShowFragment(string tag, int contentId, Bundle bundle, bo cache.GetAndClear(fragInfo.ViewModelType, GetTagFromFragment(fragInfo.CachedFragment as Fragment)); } + // Prevent to add the "same first" fragment twice + if (fragInfo.Tag == _firstFragmentTag) + { + SupportFragmentManager.PopBackStack(null, (int)PopBackStackFlags.Inclusive); + } + + if (string.IsNullOrEmpty(_firstFragmentTag)) + { + _firstFragmentTag = fragInfo.Tag; + } + if ((currentFragment != null && fragInfo.AddToBackStack) || forceAddToBackStack) { ft.AddToBackStack(fragInfo.Tag);