Java Code Examples for androidx.fragment.app.FragmentManager#getBackStackEntryAt()

The following examples show how to use androidx.fragment.app.FragmentManager#getBackStackEntryAt() . 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: 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 3
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 4
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 5
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;
}