Skip to content

Commit

Permalink
fragment回退优化
Browse files Browse the repository at this point in the history
  • Loading branch information
weikaiyun committed Oct 1, 2020
1 parent a9b7abd commit 277fd90
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,17 @@ public void onBackPressedSupport() {
List<Fragment> list = FragmentationMagician.getActiveFragments(getSupportFragmentManager());
int fragmentNum = 0;
for (Fragment f : list) {
if (f instanceof ISupportFragment) {
if (f instanceof ISupportFragment && ((ISupportFragment) f).getSupportDelegate().isCanPop()) {
fragmentNum++;
}
}
if (fragmentNum > 1) {
if (fragmentNum > 0) {
pop();
} else {
ActivityCompat.finishAfterTransition(mActivity);
}
}

/**********************************************************************************************/

/**
* 加载根Fragment, 即Activity内的第一个Fragment 或 Fragment内的第一个子Fragment
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public void onResume() {
}

if (!isHidden()) {
getSupportDelegate().getVisibleDelegate().setVisible(true);
getSupportDelegate().setVisible(true);
onVisible();
}
}

@Override
public void onPause() {
super.onPause();
getSupportDelegate().getVisibleDelegate().setVisible(false);
getSupportDelegate().setVisible(false);
onInvisible();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,31 @@ public class SupportFragmentDelegate {
protected FragmentActivity _mActivity;
private ISupportActivity mSupport;

private VisibleDelegate visibleDelegate;
private boolean isVisible;

private boolean canPop = true;

public void setCanPop(boolean canPop) {
this.canPop = canPop;
}

public boolean isCanPop() {
return canPop;
}

public void setVisible(boolean visible) {
isVisible = visible;
}

public boolean isVisible() {
return isVisible;
}

public SupportFragmentDelegate(ISupportFragment support) {
if (!(support instanceof Fragment))
throw new RuntimeException("Must extends Fragment");
this.mSupportF = support;
this.mFragment = (Fragment) support;
visibleDelegate = new VisibleDelegate();
}

public VisibleDelegate getVisibleDelegate() {
return visibleDelegate;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public void run() {
}
}

to.getSupportDelegate().setCanPop(false);
start(fm, null, to, toFragmentTag, TYPE_REPLACE);
}
});
Expand All @@ -84,6 +85,8 @@ public void run() {
for (int i = 0; i < tos.length; i++) {
Fragment to = (Fragment) tos[i];

((ISupportFragment) to).getSupportDelegate().setCanPop(false);

bindContainerId(containerId, tos[i]);

String toName = to.getClass().getName();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private void addDebugFragmentRecord(List<DebugFragmentRecord> fragmentRecords, F
if (fragment != null) {
CharSequence name = fragment.getClass().getSimpleName();

if (fragment instanceof ISupportFragment && ((ISupportFragment)fragment).getSupportDelegate().getVisibleDelegate().isVisible()) {
if (fragment instanceof ISupportFragment && ((ISupportFragment)fragment).getSupportDelegate().isVisible()) {
name = span(name, " ☀");
}

Expand Down

0 comments on commit 277fd90

Please sign in to comment.