Skip to content

Commit

Permalink
Merge pull request #2482 from Qwin/supportnestedfragments
Browse files Browse the repository at this point in the history
[WIP] Added support for nested fragments (ChildFragmentManager)
  • Loading branch information
martijn00 authored Jan 10, 2018
2 parents a5f61fd + 569e311 commit 4a1fae7
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ public override Object InstantiateItem(ViewGroup container, int position)
if (fss != null)
fragment.SetInitialSavedState(fss);

//if fragment tag is null let's set it to something meaning full;
if (string.IsNullOrEmpty(fragmentTag))
{
fragmentTag = FragmentJavaName(fragment.GetType());
}

#if DEBUG
Mvx.Trace("Adding item #{0}: f={1} t={2}", position, fragment, fragmentTag);
#endif
Expand All @@ -116,6 +122,11 @@ public override Object InstantiateItem(ViewGroup container, int position)
return fragment;
}

protected string FragmentJavaName(Type fragmentType)
{
return Class.FromType(fragmentType).Name;
}

public override bool IsViewFromObject(View view, Object objectValue)
{
return ((Fragment)objectValue).View == view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public MvxCachingFragmentStatePagerAdapter(Context context, FragmentManager frag

public List<MvxViewPagerFragmentInfo> FragmentsInfo { get; }

protected static string FragmentJavaName(Type fragmentType)
protected string FragmentJavaName(Type fragmentType)
{
return Class.FromType(fragmentType).Name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,36 @@ protected override IMvxFragmentView CreateFragment(MvxBasePresentationAttribute
protected virtual new Fragment GetFragmentByViewType(Type type)
{
var fragmentName = FragmentJavaName(type);
return CurrentFragmentManager?.FindFragmentByTag(fragmentName);
var fragment = CurrentFragmentManager?.FindFragmentByTag(fragmentName);

if (fragment != null)
{
return fragment;
}

return FindFragmentInChildren(fragmentName, CurrentFragmentManager);
}

protected virtual Fragment FindFragmentInChildren(string fragmentName, FragmentManager fragManager)
{
if (fragManager?.Fragments != null && !fragManager.Fragments.Any()) return null;

foreach (var fragment in fragManager.Fragments)
{
//let's try again finding it
var frag = fragment?.ChildFragmentManager?.FindFragmentByTag(fragmentName);

//if we found the frag lets return it!
if (frag != null)
{
return frag;
}

//reloop for other fragments
FindFragmentInChildren(fragmentName, fragment?.ChildFragmentManager);
}

return null;
}
}
}
34 changes: 33 additions & 1 deletion MvvmCross/Droid/Droid/Views/MvxAndroidViewPresenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -570,7 +570,39 @@ protected virtual IMvxFragmentView CreateFragment(MvxBasePresentationAttribute a
protected virtual Fragment GetFragmentByViewType(Type type)
{
var fragmentName = FragmentJavaName(type);
return CurrentFragmentManager.FindFragmentByTag(fragmentName);
var fragment = CurrentFragmentManager?.FindFragmentByTag(fragmentName);

if (fragment != null)
{
return fragment;
}

return FindFragmentInChildren(fragmentName, CurrentFragmentManager);
}

protected virtual Fragment FindFragmentInChildren(string fragmentName, FragmentManager fragManager)
{
if(fragManager.BackStackEntryCount == 0)
return null;

for (int i = 0; i < fragManager.BackStackEntryCount; i++)
{
var parentFrag = fragManager.FindFragmentById(fragManager.GetBackStackEntryAt(i).Id);

//let's try again finding it
var frag = parentFrag?.ChildFragmentManager?.FindFragmentByTag(fragmentName);

//if we found the frag lets return it!
if (frag != null)
{
return frag;
}

//reloop for other fragments
FindFragmentInChildren(fragmentName, parentFrag?.ChildFragmentManager);
}

return null;
}
}
}

0 comments on commit 4a1fae7

Please sign in to comment.