Java Code Examples for androidx.fragment.app.FragmentManager#BackStackEntry

The following examples show how to use androidx.fragment.app.FragmentManager#BackStackEntry . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AwesomeActivity.java    From AndroidNavigation with MIT License 6 votes vote down vote up
@Override
public void onBackPressed() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    int count = fragmentManager.getBackStackEntryCount();
    if (count > 0) {
        FragmentManager.BackStackEntry entry = fragmentManager.getBackStackEntryAt(count - 1);
        AwesomeFragment fragment = (AwesomeFragment) fragmentManager.findFragmentByTag(entry.getName());
        if (fragment != null && fragment.isAdded() && !fragment.dispatchBackPressed()) {
            if (count == 1) {
                if (!handleBackPressed()) {
                    ActivityCompat.finishAfterTransition(this);
                }
            } else {
                dismissFragment(fragment);
            }
        }
    } else {
        super.onBackPressed();
    }
}
 
Example 2
Source File: MainActivity.java    From fastnfitness with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void onBackPressed() {
    int index = getActivity().getSupportFragmentManager().getBackStackEntryCount() - 1;
    if (index >= 0) { // Si on est dans une sous activité
        FragmentManager.BackStackEntry backEntry = getSupportFragmentManager().getBackStackEntryAt(index);
        String tag = backEntry.getName();
        Fragment fragment = getSupportFragmentManager().findFragmentByTag(tag);
        super.onBackPressed();
        getActivity().getSupportActionBar().show();
    } else { // Si on est la racine, avec il faut cliquer deux fois
        if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis()) {
            super.onBackPressed();
            return;
        } else {
            Toast.makeText(getBaseContext(), R.string.pressBackAgain, Toast.LENGTH_SHORT).show();
        }

        mBackPressed = System.currentTimeMillis();
    }
}
 
Example 3
Source File: MapActivity.java    From nearby-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onBackPressed() {
  int count = getSupportFragmentManager().getBackStackEntryCount();
  if (count == 0) {
    super.onBackPressed();
  } else {
    getSupportFragmentManager().popBackStack();
    FragmentManager.BackStackEntry entry = getSupportFragmentManager().getBackStackEntryAt(count-1);
    String fragmentName = entry.getName();
    if (fragmentName.equalsIgnoreCase(getString(R.string.route_fragment))){
      restoreRouteView();
    }else{
      finish();
    }
  }
}
 
Example 4
Source File: NavigationFragment.java    From AndroidNavigation with MIT License 5 votes vote down vote up
@Nullable
public AwesomeFragment getRootFragment() {
    if (isAdded()) {
        FragmentManager fragmentManager = getChildFragmentManager();
        int count = fragmentManager.getBackStackEntryCount();
        if (count > 0) {
            FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(0);
            return (AwesomeFragment) fragmentManager.findFragmentByTag(backStackEntry.getName());
        }
    }
    return rootFragment;
}
 
Example 5
Source File: AwesomeFragment.java    From AndroidNavigation with MIT License 5 votes vote down vote up
@Nullable
public AwesomeFragment getPresentedFragment() {
    AwesomeFragment parent = getParentAwesomeFragment();
    if (parent != null) {
        if (definesPresentationContext()) {
            if (FragmentHelper.findIndexAtBackStack(requireFragmentManager(), this) == -1) {
                if (parent.getChildFragmentCountAtBackStack() == 0) {
                    return null;
                } else {
                    FragmentManager.BackStackEntry backStackEntry = requireFragmentManager().getBackStackEntryAt(0);
                    AwesomeFragment presented = (AwesomeFragment) requireFragmentManager().findFragmentByTag(backStackEntry.getName());
                    if (presented != null && presented.isAdded()) {
                        return presented;
                    }
                    return null;
                }
            } else {
                return FragmentHelper.getLatterFragment(requireFragmentManager(), this);
            }
        } else {
            return parent.getPresentedFragment();
        }
    }

    if (presentableActivity != null) {
        return presentableActivity.getPresentedFragment(this);
    }

    return null;
}
 
Example 6
Source File: FragmentHelper.java    From AndroidNavigation with MIT License 5 votes vote down vote up
@Nullable
public static AwesomeFragment getLatterFragment(@NonNull FragmentManager fragmentManager, @NonNull AwesomeFragment fragment) {
    int count = fragmentManager.getBackStackEntryCount();
    int index = findIndexAtBackStack(fragmentManager, fragment);
    if (index > -1 && index < count - 1) {
        FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(index + 1);
        AwesomeFragment latter = (AwesomeFragment) fragmentManager.findFragmentByTag(backStackEntry.getName());
        if (latter != null && latter.isAdded()) {
            return latter;
        }
    }
    return null;
}
 
Example 7
Source File: FragmentHelper.java    From AndroidNavigation with MIT License 5 votes vote down vote up
@Nullable
public static AwesomeFragment getAheadFragment(@NonNull FragmentManager fragmentManager, @NonNull AwesomeFragment fragment) {
    int count = fragmentManager.getBackStackEntryCount();
    int index = findIndexAtBackStack(fragmentManager, fragment);
    if (index > 0 && index < count) {
        FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(index - 1);
        AwesomeFragment ahead = (AwesomeFragment) fragmentManager.findFragmentByTag(backStackEntry.getName());
        if (ahead != null && ahead.isAdded()) {
            return ahead;
        }
    }
    return null;
}
 
Example 8
Source File: FragmentHelper.java    From AndroidNavigation with MIT License 5 votes vote down vote up
public static int findIndexAtBackStack(@NonNull FragmentManager fragmentManager, @NonNull AwesomeFragment fragment) {
    int count = fragmentManager.getBackStackEntryCount();
    int index = -1;
    for (int i = 0; i < count; i++) {
        FragmentManager.BackStackEntry backStackEntry = fragmentManager.getBackStackEntryAt(i);
        String tag = fragment.getTag();
        if (tag != null && tag.equals(backStackEntry.getName())) {
            index = i;
        }
    }
    return index;
}
 
Example 9
Source File: FragmentResultNavigator.java    From aptoide-client-v8 with GNU General Public License v3.0 5 votes vote down vote up
@Override public Fragment peekLast() {
  if (fragmentManager.getBackStackEntryCount() > 0) {
    FragmentManager.BackStackEntry backStackEntry =
        fragmentManager.getBackStackEntryAt(fragmentManager.getBackStackEntryCount() - 1);
    return fragmentManager.findFragmentByTag(backStackEntry.getName());
  }

  return null;
}