Java Code Examples for android.support.v4.app.FragmentManager#getBackStackEntryCount()

The following examples show how to use android.support.v4.app.FragmentManager#getBackStackEntryCount() . 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: BackHandlerHelper.java    From FragmentBackHandler with Apache License 2.0 6 votes vote down vote up
/**
 * 将back事件分发给 FragmentManager 中管理的子Fragment,如果该 FragmentManager 中的所有Fragment都
 * 没有处理back事件,则尝试 FragmentManager.popBackStack()
 *
 * @return 如果处理了back键则返回 <b>true</b>
 * @see #handleBackPress(Fragment)
 * @see #handleBackPress(FragmentActivity)
 */
public static boolean handleBackPress(FragmentManager fragmentManager) {
    List<Fragment> fragments = fragmentManager.getFragments();

    if (fragments == null) return false;

    for (int i = fragments.size() - 1; i >= 0; i--) {
        Fragment child = fragments.get(i);

        if (isFragmentBackHandled(child)) {
            return true;
        }
    }

    if (fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStack();
        return true;
    }
    return false;
}
 
Example 2
Source File: NavigatorUtils.java    From Navigator with Apache License 2.0 6 votes vote down vote up
/**
 * @param tag             point to return
 * @param container       id container
 * @param fragmentManager variable contain fragment stack
 * @return true if is possible return to tag param
 */
public boolean canGoBackToSpecificPoint(String tag, int container, FragmentManager fragmentManager) {
    if (TextUtils.isEmpty(tag)) {
        return (fragmentManager.getBackStackEntryCount() > 1);
    } else {
        List<FragmentManager.BackStackEntry> fragmentList = fragmentList();
        Fragment fragment = fragmentManager.findFragmentById(container);
        if (fragment != null && tag.equalsIgnoreCase(fragment.getTag())) {
            return false;
        }
        for (int i = 0; i < fragmentList.size(); i++) {
            if (tag.equalsIgnoreCase(fragmentList.get(i).getName())) {
                return true;
            }
        }
        return false;
    }
}
 
Example 3
Source File: Wizard.java    From Wizard with Apache License 2.0 6 votes vote down vote up
public boolean navigateNext() {
  if (!activity.isFinishing()) {
    FragmentManager fragmentManager = activity.getSupportFragmentManager();
    int nextStep = fragmentManager.getBackStackEntryCount() + 1;
    if (nextStep < pages.length) {
      WizardPage WizardPage = pages[nextStep];
      Fragment fragment = WizardPage.createFragment();
      fragmentManager.beginTransaction()
          .addToBackStack(fragment.getClass().getName())
          .setCustomAnimations(enterAnimation, exitAnimation, popEnterAnimation, popExitAnimation)
          .replace(containerId, fragment)
          .commit();
      fragmentManager.executePendingTransactions();
      return true;
    } else if (wizardListener != null) {
      wizardListener.onWizardFinished();
    }
  }
  return false;
}
 
Example 4
Source File: MainActivity.java    From rcloneExplorer with MIT License 6 votes vote down vote up
private void startPinnedRemote(RemoteItem remoteItem) {
    if (fragment != null && fragment instanceof FileExplorerFragment) {
        FragmentManager fragmentManager = getSupportFragmentManager();

        // this is the case when remote gets started from a shortcut
        // therefore back should exit the app, and not go into remotes screen
        if (fragmentManager.getBackStackEntryCount() == 0) {
            startRemote(remoteItem, false);
        } else {
            for (int i = 0; i < fragmentManager.getBackStackEntryCount(); i++) {
                fragmentManager.popBackStack();
            }

            startRemote(remoteItem, true);
        }
    } else {
        startRemote(remoteItem, true);
    }

    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
}
 
Example 5
Source File: MainActivity.java    From FacebookNewsfeedSample-Android with Apache License 2.0 6 votes vote down vote up
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
    if (isResumed) {
        FragmentManager manager = getSupportFragmentManager();
        int backStackSize = manager.getBackStackEntryCount();
        for (int i = 0; i < backStackSize; i++) {
            manager.popBackStack();
        }
        // check for the OPENED state instead of session.isOpened() since for the
        // OPENED_TOKEN_UPDATED state, the selection fragment should already be showing.
        if (state.equals(SessionState.OPENED)) {
            showFragment(SELECTION, false);
        } else if (state.isClosed()) {
            showFragment(SPLASH, false);
        }
    }
}
 
Example 6
Source File: Navigator.java    From Pioneer with Apache License 2.0 6 votes vote down vote up
public static boolean hasBackStackEntry(FragmentManager fragmentManager) {
    int count = fragmentManager.getBackStackEntryCount();
    if (count > 0) {
        return true;
    }
    if (isFragmentInBackStack != null) {
        List<Fragment> fragments = fragmentManager.getFragments();
        if (fragments != null) {
            for (Fragment frag : fragments) {
                if (frag != null && isInBackStack(frag)) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 7
Source File: MainActivity.java    From bubble with MIT License 5 votes vote down vote up
private boolean popFragment() {
    FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStack();
        return true;
    }
    return false;
}
 
Example 8
Source File: DisplayActivity.java    From AndroidTrainingCode with Apache License 2.0 5 votes vote down vote up
@Override
public void onBackStackChanged() {
    
    // Gets the previous global stack count
    int previousStackCount = mPreviousStackCount;
    
    // Gets a FragmentManager instance
    FragmentManager localFragmentManager = getSupportFragmentManager();
    
    // Sets the current back stack count
    int currentStackCount = localFragmentManager.getBackStackEntryCount();
    
    // Re-sets the global stack count to be the current count
    mPreviousStackCount = currentStackCount;
    
    /*
     * If the current stack count is less than the previous, something was popped off the stack
     * probably because the user clicked Back.
     */
    boolean popping = currentStackCount < previousStackCount;
    Log.d(CLASS_TAG, "backstackchanged: popping = " + popping);
    
    // When going backwards in the back stack, turns off full screen mode.
    if (popping) {
        setFullScreen(false);
    }
}
 
Example 9
Source File: ApplicationPreferencesActivity.java    From Silence with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onSupportNavigateUp() {
  FragmentManager fragmentManager = getSupportFragmentManager();
  if (fragmentManager.getBackStackEntryCount() > 0) {
    fragmentManager.popBackStack();
  } else {
    Intent intent = new Intent(this, ConversationListActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivity(intent);
    finish();
  }
  return true;
}
 
Example 10
Source File: MainActivity.java    From product-catalogue-android with MIT License 5 votes vote down vote up
private void clearBackstack() {
  FragmentManager fm = getSupportFragmentManager();

  if (fm.getBackStackEntryCount() > 0) {
    fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
  }
}
 
Example 11
Source File: FragmentUtils.java    From styT with Apache License 2.0 5 votes vote down vote up
/**
 * 出栈所有fragment
 *
 * @param fragmentManager fragment管理器
 */
public static void popAllFragments(@NonNull final FragmentManager fragmentManager) {
    List<Fragment> fragments = getFragments(fragmentManager);
    if (fragments.isEmpty()) return;
    for (int i = fragments.size() - 1; i >= 0; --i) {
        Fragment fragment = fragments.get(i);
        if (fragment != null) popAllFragments(fragment.getChildFragmentManager());
    }
    while (fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStackImmediate();
    }
}
 
Example 12
Source File: Wizard.java    From Wizard with Apache License 2.0 5 votes vote down vote up
public boolean navigatePrevious() {
  if (!activity.isFinishing()) {
    FragmentManager fragmentManager = activity.getSupportFragmentManager();
    if (fragmentManager.getBackStackEntryCount() > 0) {
      fragmentManager.popBackStackImmediate();
      fragmentManager.executePendingTransactions();
      return true;
    }
  }
  return false;
}
 
Example 13
Source File: MainActivity.java    From droidkaigi2016 with Apache License 2.0 5 votes vote down vote up
@Override
public void onBackPressed() {
    if (binding.drawer.isDrawerOpen(GravityCompat.START)) {
        binding.drawer.closeDrawer(GravityCompat.START);
        return;
    }
    FragmentManager fm = getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        fm.popBackStack();
        return;
    }
    super.onBackPressed();
}
 
Example 14
Source File: BrowsingActivityNavigationControllerSinglePane.java    From IslamicLibraryAndroid with GNU General Public License v3.0 5 votes vote down vote up
public BrowsingActivityNavigationControllerSinglePane(int oldPanNumbers, @NonNull FragmentManager fragmentManager, boolean fromRotation, BrowsingActivity browsingActivity, @NonNull BottomNavigationView bottomNavigationView, @NonNull BrowsingActivityControllerListener listener) {
    super(oldPanNumbers, fragmentManager, fromRotation, browsingActivity, bottomNavigationView, listener);
    paneNumber = 1;
    backStackChangedListener = () -> {
        if (fragmentManager.getBackStackEntryCount() == 0) {
            bottomNavigationView.setVisibility(View.VISIBLE);
            listener.setUpNavigation(false);
        } else {
            bottomNavigationView.setVisibility(View.GONE);
            listener.setUpNavigation(true);
        }
    };
    fragmentManager.addOnBackStackChangedListener(backStackChangedListener);
}
 
Example 15
Source File: ActivityUtils.java    From incubator-taverna-mobile with Apache License 2.0 5 votes vote down vote up
public static void addFragmentToActivity(FragmentManager fragmentManager, Fragment fragment,
                                         int frameId) {

    for (int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
        fragmentManager.popBackStack();
    }
    FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(frameId, fragment);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
    transaction.commit();
}
 
Example 16
Source File: CubeFragmentActivity.java    From cube-sdk with Apache License 2.0 5 votes vote down vote up
public void popToRoot(Object data) {
    FragmentManager fm = getSupportFragmentManager();
    while (fm.getBackStackEntryCount() > 1) {
        fm.popBackStackImmediate();
    }
    popTopFragment(data);
}
 
Example 17
Source File: CubeFragmentActivity.java    From cube-sdk with Apache License 2.0 5 votes vote down vote up
private boolean tryToUpdateCurrentAfterPop() {
    FragmentManager fm = getSupportFragmentManager();
    int cnt = fm.getBackStackEntryCount();
    if (cnt > 0) {
        String name = fm.getBackStackEntryAt(cnt - 1).getName();
        Fragment fragment = fm.findFragmentByTag(name);
        if (fragment != null && fragment instanceof CubeFragment) {
            mCurrentFragment = (CubeFragment) fragment;
        }
        return true;
    }
    return false;
}
 
Example 18
Source File: DynamicActivity.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
@Override
public void onBackPressed() {
    FragmentManager fm = this.getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 1) {
        fm.popBackStack();
    } else {
        finish();
    }
}
 
Example 19
Source File: FragmentUtils.java    From Android-UtilCode with Apache License 2.0 4 votes vote down vote up
/**
 * 出栈同级别fragment
 *
 * @param fragmentManager fragment管理器
 */
public static void popFragments(@NonNull FragmentManager fragmentManager) {
    while (fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStackImmediate();
    }
}
 
Example 20
Source File: FragmentUtils.java    From styT with Apache License 2.0 4 votes vote down vote up
/**
 * 出栈同级别fragment
 *
 * @param fragmentManager fragment管理器
 */
public static void popFragments(@NonNull final FragmentManager fragmentManager) {
    while (fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStackImmediate();
    }
}