Skip to content

Commit

Permalink
Fix #473, fixes an onHiddenChanged() error callback when starting mul…
Browse files Browse the repository at this point in the history
…tiple Fragments at the same time
  • Loading branch information
YoKeyword committed Aug 1, 2017
1 parent 256f681 commit a607f05
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,22 @@ public static void logFragmentStackHierarchy(ISupportActivity support, String TA
* 获得栈顶SupportFragment
*/
public static ISupportFragment getTopFragment(FragmentManager fragmentManager) {
return getTopFragment(fragmentManager, 0);
}

public static ISupportFragment getTopFragment(FragmentManager fragmentManager, int containerId) {
List<Fragment> fragmentList = FragmentationHack.getActiveFragments(fragmentManager);
if (fragmentList == null) return null;

for (int i = fragmentList.size() - 1; i >= 0; i--) {
Fragment fragment = fragmentList.get(i);
if (fragment instanceof ISupportFragment) {
return (ISupportFragment) fragment;
ISupportFragment iFragment = (ISupportFragment) fragment;
if (containerId == 0) return iFragment;

if (containerId == iFragment.getSupportDelegate().mContainerId) {
return iFragment;
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,14 @@ void dispatchStartTransaction(FragmentManager fragmentManager, ISupportFragment
checkNotNull(to, "toFragment == null");

if (from != null) {
Fragment fromF = (Fragment) from;
if (from.getSupportDelegate().mContainerId == 0) {
Fragment fromF = (Fragment) from;
if (fromF.getTag() != null && !fromF.getTag().startsWith("android:switcher:")) {
throw new RuntimeException("Can't find container, please call loadRootFragment() first!");
}
}
bindContainerId(from.getSupportDelegate().mContainerId, to);
from = SupportHelper.getTopFragment(fragmentManager, from.getSupportDelegate().mContainerId);
}

// process SupportTransaction
Expand All @@ -135,8 +136,7 @@ void dispatchStartTransaction(FragmentManager fragmentManager, ISupportFragment
saveRequestCode((Fragment) to, requestCode);
}

if (handleLaunchMode(fragmentManager, to, toFragmentTag, launchMode))
return;
if (handleLaunchMode(fragmentManager, from, to, toFragmentTag, launchMode)) return;

if (type == TYPE_ADD_WITH_POP) {
startWithPop(fragmentManager, from, to);
Expand Down Expand Up @@ -300,8 +300,7 @@ boolean dispatchBackPressedEvent(ISupportFragment activeFragment) {
/**
* handle LaunchMode
*/
private boolean handleLaunchMode(FragmentManager fragmentManager, final ISupportFragment to, String toFragmentTag, int launchMode) {
ISupportFragment topFragment = getTopFragment(fragmentManager);
private boolean handleLaunchMode(FragmentManager fragmentManager, ISupportFragment topFragment, final ISupportFragment to, String toFragmentTag, int launchMode) {
if (topFragment == null) return false;
final ISupportFragment stackToFragment = SupportHelper.findStackFragment(to.getClass(), toFragmentTag, fragmentManager);
if (stackToFragment == null) return false;
Expand Down

0 comments on commit a607f05

Please sign in to comment.