Skip to content
This repository has been archived by the owner. It is now read-only.

Prevent Fragment Added Exception #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions MvvmCross.Droid.Support.V4/MvxCachingFragmentActivity.cs
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
Expand Down