public Fragment getCurrentFragment() { final int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount(); if (backStackEntryCount > 0) { try { final BackStackEntry backStackEntryAt = getSupportFragmentManager().getBackStackEntryAt(backStackEntryCount - 1); String fragmentTag = backStackEntryAt.getName(); Fragment currentFragment = getSupportFragmentManager().findFragmentByTag(fragmentTag); return currentFragment; } catch (Exception e) { // fix possible race condition: // when called in background thread - back stack could be different between call of // getBackStackEntryCount() and getBackStackEntryAt() e.printStackTrace(); } } return getSupportFragmentManager().findFragmentByTag(AppConstants.PAGE_TITLE_MAIN); }
@Override public void onBackStackChanged() { if (getFragmentManager() == null) { Log.w(TAG, "getFragmentManager() is null, stack:", new Exception()); return; } int count = getFragmentManager().getBackStackEntryCount(); // if backstack is growing and last pushed entry is "headers" backstack, // remember the index of the entry. if (count > mLastEntryCount) { BackStackEntry entry = getFragmentManager().getBackStackEntryAt(count - 1); if (mWithHeadersBackStackName.equals(entry.getName())) { mIndexOfHeadersBackStack = count - 1; } } else if (count < mLastEntryCount) { // if popped "headers" backstack, initiate the show header transition if needed if (mIndexOfHeadersBackStack >= count) { mIndexOfHeadersBackStack = -1; if (!mShowingHeaders) { startHeadersTransitionInternal(true); } } } mLastEntryCount = count; }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) @Override public void onBackStackChanged() { int count = mFragmentManager.getBackStackEntryCount(); if (count > 0) { BackStackEntry fragment = mFragmentManager .getBackStackEntryAt(count - 1); mPath = fragment.getName(); } else { mPath = EXTERNAL_BASE_PATH; } setTitle(mPath); if (HAS_ACTIONBAR) invalidateOptionsMenu(); }
@Override public void onClick(View v) { if (v.getTag() instanceof BackStackEntry) { BackStackEntry bse = (BackStackEntry) v.getTag(); if (bse == mParentEntry) { if (mParentClickListener != null) { mParentClickListener.onClick(v); } } else { if (mOnBreadCrumbClickListener != null) { if (mOnBreadCrumbClickListener.onBreadCrumbClick( bse == mTopEntry ? null : bse, 0)) { return; } } if (bse == mTopEntry) { mActivity.getSupportFragmentManager().popBackStack(); } else { mActivity.getSupportFragmentManager().popBackStack( bse.getId(), 0); } } } }
/** * Get latest attached fragment from {@link FragmentManager} * @return Latest attached Fragment */ public Fragment getTopFragment() { FragmentManager fragMgr = getSupportFragmentManager(); if (fragMgr.getBackStackEntryCount() == 0) { return null; } FragmentManager.BackStackEntry backEntry = fragMgr.getBackStackEntryAt(fragMgr.getBackStackEntryCount() - 1); String backEntryName = backEntry.getName(); return fragMgr.findFragmentByTag(backEntryName); }
@Override public void onBackStackChanged() { int count = mFragmentManager.getBackStackEntryCount(); if (count > 0) { BackStackEntry fragment = mFragmentManager.getBackStackEntryAt(count - 1); mPath = fragment.getName(); } else { mPath = EXTERNAL_BASE_PATH; } setTitle(mPath); if (HAS_ACTIONBAR) invalidateOptionsMenu(); }
public void onBackStackChanged() { path = EXTERNAL_BASE_PATH; int count = fragmentManager.getBackStackEntryCount(); if (count > 0) { BackStackEntry fragment = fragmentManager.getBackStackEntryAt(count - 1); path = fragment.getName(); } setTitle(path); }
private void startHeadersTransitionInternal(final boolean withHeaders) { if (getFragmentManager().isDestroyed()) { return; } mShowingHeaders = withHeaders; mRowsSupportFragment.onExpandTransitionStart(!withHeaders, new Runnable() { @Override public void run() { mHeadersSupportFragment.onTransitionStart(); createHeadersTransition(); if (mBrowseTransitionListener != null) { mBrowseTransitionListener.onHeadersTransitionStart(withHeaders); } sTransitionHelper.runTransition(withHeaders ? mSceneWithHeaders : mSceneWithoutHeaders, mHeadersTransition); if (mHeadersBackStackEnabled) { if (!withHeaders) { getFragmentManager().beginTransaction() .addToBackStack(mWithHeadersBackStackName).commit(); } else { int index = mBackStackChangedListener.mIndexOfHeadersBackStack; if (index >= 0) { BackStackEntry entry = getFragmentManager().getBackStackEntryAt(index); getFragmentManager().popBackStackImmediate(entry.getId(), FragmentManager.POP_BACK_STACK_INCLUSIVE); } } } } }); }
@Override public void onBackStackChanged() { int count = mFragmentManager.getBackStackEntryCount(); if (count > 0) { BackStackEntry fragment = mFragmentManager .getBackStackEntryAt(count - 1); mPath = fragment.getName(); } else { mPath = EXTERNAL_BASE_PATH; } setTitle(mPath); supportInvalidateOptionsMenu(); }
@Override public void onBackStackChanged() { int count = mFragmentManager.getBackStackEntryCount(); if (count > 0) { BackStackEntry fragment = mFragmentManager.getBackStackEntryAt(count - 1); mPath = fragment.getName(); } else { mPath = EXTERNAL_BASE_PATH; } setTitle(mPath); if (HAS_ACTIONBAR) supportInvalidateOptionsMenu(); }
private BackStackEntry createBackStackEntry(final CharSequence title, final CharSequence shortTitle) { if (title == null) { return null; } return new BackStackEntry() { @Override public CharSequence getBreadCrumbShortTitle() { return shortTitle; } @Override public int getBreadCrumbShortTitleRes() { return 0; } @Override public CharSequence getBreadCrumbTitle() { return title; } @Override public int getBreadCrumbTitleRes() { return 0; } @Override public int getId() { return 2837452; } @Override public String getName() { return "backstackentry"; } }; }
private BackStackEntry getPreEntry(int index) { if (mParentEntry != null) { return index == 0 ? mParentEntry : mTopEntry; } else { return mTopEntry; } }
@Override public void onBackPressed() { FragmentManager mgr = getSupportFragmentManager(); int idxLast = mgr.getBackStackEntryCount() - 2; if(idxLast < 0) { this.setResult(RESULT_OK); finish(); } else { BackStackEntry entry = mgr.getBackStackEntryAt(idxLast); swapToFragmentView(ScreenList.valueOf(entry.getName())); } }
@Override public void onBackStackChanged() { mPath = EXTERNAL_BASE_PATH; int count = mFragmentManager.getBackStackEntryCount(); if (count > 0) { BackStackEntry fragment = mFragmentManager .getBackStackEntryAt(count - 1); mPath = fragment.getName(); } setTitle(mPath); }
public BackStackEntry getBackStackEntryAt(int index) { return (BackStackEntry) this.mBackStack.get(index); }
/** * When {@link #setDefaultBackStackListener()} ran in {@link #onCreate(Bundle)},<br/> * this method will execute on every back stack change. */ protected void BackStackSyncStatus() { try { FragmentManager fragMgr = getSupportFragmentManager(); int count = fragMgr.getBackStackEntryCount(); if (count - 1 >= 0) { BackStackEntry entry = fragMgr.getBackStackEntryAt(count - 1); String title = (String) entry.getBreadCrumbTitle(); if (getSupportActionBar() != null) { setBackStackTitle(title); getSupportActionBar().setHomeButtonEnabled(true); if (!hasLeftDrawer && !hasRightDrawer) { getSupportActionBar().setDisplayHomeAsUpEnabled(true); } } if (drawerBehavior == DRAWER_MAIN_ONLY && drawerLayout != null) { drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.START); drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, GravityCompat.END); } if (drawerToggle != null) { drawerToggle.setDrawerIndicatorEnabled(false); } onBaseBackStackChanged(false, count); } else { if (getSupportActionBar() != null) { setBackStackTitle(Utility.getAppName(this)); getSupportActionBar().setHomeButtonEnabled(false); if (!hasLeftDrawer && !hasRightDrawer) { getSupportActionBar().setDisplayHomeAsUpEnabled(false); } } if (hasLeftDrawer) { drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.START); if (getDrawerToggle() != null) { getDrawerToggle().setDrawerIndicatorEnabled(true); } } if (hasRightDrawer) { drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, GravityCompat.END); } onBaseBackStackChanged(true, count); } if (drawerToggle != null) { drawerToggle.syncState(); } } catch (Exception e) { e.printStackTrace(); } }
/** * Add a fragment to targeted FrameLayout. * @param viewId Targeted FrameLayout resource id * @param fragment Fragment */ public void addFragment(int viewId, Fragment fragment) { if (fragment.isAdded()) { return; } FragmentManager fragMgr = getSupportFragmentManager(); FragmentTransaction transaction = fragMgr.beginTransaction(); BackStackEntry lastEntry = fragMgr.getBackStackEntryCount() == 0 ? null : fragMgr.getBackStackEntryAt(fragMgr.getBackStackEntryCount() - 1); // Transaction options String fragName = fragment.getClass().getSimpleName(); String breadCrumbTitle; String breadCrumbShortTitle; Boolean addToBackStack = false; int animationEnter = 0; int animationExit = 0; // Fill options Bundle args = fragment.getArguments(); if (args != null) { if (args.containsKey(BundleArgs.animationEnter) && args.containsKey(BundleArgs.animationExit)) { animationEnter = args.getInt(BundleArgs.animationEnter); animationExit = args.getInt(BundleArgs.animationExit); } if (args.containsKey(BundleArgs.breadCrumbTitle)) { breadCrumbTitle = args.getString(BundleArgs.breadCrumbTitle); } else { breadCrumbTitle = (lastEntry != null) ? lastEntry.getBreadCrumbTitle().toString() : Utility.getAppName(this); } if (args.containsKey(BundleArgs.breadCrumbShortTitle)) { breadCrumbShortTitle = args.getString(BundleArgs.breadCrumbShortTitle); } else { breadCrumbShortTitle = (lastEntry != null) ? lastEntry.getBreadCrumbShortTitle().toString() : Utility.getAppName(this); } if (args.containsKey(BundleArgs.fragmentName)) { fragName = args.getString(BundleArgs.fragmentName); } if (args.containsKey(BundleArgs.addToBackStack)) { addToBackStack = args.getBoolean(BundleArgs.addToBackStack); } } else { breadCrumbTitle = (lastEntry != null) ? lastEntry.getBreadCrumbTitle().toString() : Utility.getAppName(this); breadCrumbShortTitle = (lastEntry != null) ? lastEntry.getBreadCrumbShortTitle().toString() : Utility.getAppName(this); } // Set option to fragment manager if (animationEnter != 0 && animationExit != 0) { transaction.setCustomAnimations(animationEnter, animationExit); } if (breadCrumbTitle != null) { transaction.setBreadCrumbTitle(breadCrumbTitle); } if (breadCrumbShortTitle != null) { transaction.setBreadCrumbShortTitle(breadCrumbShortTitle); } if (transaction.isAddToBackStackAllowed() && addToBackStack) { transaction.addToBackStack(fragName); } // Add fragment transaction.add(viewId, fragment, fragName).commitAllowingStateLoss(); }
/** * Replace a fragment from targeted FrameLayout with another fragment. * @param viewId Targeted FrameLayout resource id * @param fragment Fragment */ public void replaceFragment(int viewId, Fragment fragment) { if (fragment.isAdded()) { return; } FragmentManager fragMgr = getSupportFragmentManager(); FragmentTransaction transaction = fragMgr.beginTransaction(); BackStackEntry lastEntry = fragMgr.getBackStackEntryCount() == 0 ? null : fragMgr.getBackStackEntryAt(fragMgr.getBackStackEntryCount() - 1); // Transaction options String fragName = fragment.getClass().getSimpleName(); String breadCrumbTitle; String breadCrumbShortTitle; Boolean addToBackStack = true; int animationEnter = 0; int animationExit = 0; // Fill options Bundle args = fragment.getArguments(); if (args != null) { if (args.containsKey(BundleArgs.animationEnter) && args.containsKey(BundleArgs.animationExit)) { animationEnter = args.getInt(BundleArgs.animationEnter); animationExit = args.getInt(BundleArgs.animationExit); } if (args.containsKey(BundleArgs.breadCrumbTitle)) { breadCrumbTitle = args.getString(BundleArgs.breadCrumbTitle); } else { breadCrumbTitle = (lastEntry != null) ? lastEntry.getBreadCrumbTitle().toString() : Utility.getAppName(this); } if (args.containsKey(BundleArgs.breadCrumbShortTitle)) { breadCrumbShortTitle = args.getString(BundleArgs.breadCrumbShortTitle); } else { breadCrumbShortTitle = (lastEntry != null) ? lastEntry.getBreadCrumbShortTitle().toString() : Utility.getAppName(this); } if (args.containsKey(BundleArgs.fragmentName)) { fragName = args.getString(BundleArgs.fragmentName); } if (args.containsKey(BundleArgs.addToBackStack)) { addToBackStack = args.getBoolean(BundleArgs.addToBackStack); } } else { breadCrumbTitle = (lastEntry != null) ? lastEntry.getBreadCrumbTitle().toString() : Utility.getAppName(this); breadCrumbShortTitle = (lastEntry != null) ? lastEntry.getBreadCrumbShortTitle().toString() : Utility.getAppName(this); } // Set option to fragment manager if (animationEnter != 0 && animationExit != 0) { transaction.setCustomAnimations(animationEnter, animationExit); } if (breadCrumbTitle != null) { transaction.setBreadCrumbTitle(breadCrumbTitle); } if (breadCrumbShortTitle != null) { transaction.setBreadCrumbShortTitle(breadCrumbShortTitle); } if (transaction.isAddToBackStackAllowed() && addToBackStack) { transaction.addToBackStack(fragName); } // Replace fragment transaction.replace(viewId, fragment, fragName).commitAllowingStateLoss(); }
void updateCrumbs() { FragmentManager fm = mActivity.getSupportFragmentManager(); int numEntries = fm.getBackStackEntryCount(); int numPreEntries = getPreEntryCount(); int numViews = mContainer.getChildCount(); for (int i = 0; i < numEntries + numPreEntries; i++) { BackStackEntry bse = i < numPreEntries ? getPreEntry(i) : fm .getBackStackEntryAt(i - numPreEntries); if (i < numViews) { View v = mContainer.getChildAt(i); Object tag = v.getTag(); if (tag != bse) { for (int j = i; j < numViews; j++) { mContainer.removeViewAt(i); } numViews = i; } } if (i >= numViews) { final View item = mInflater.inflate( R.layout.fragment_bread_crumb_item, this, false); final TextView text = (TextView) item.findViewById(R.id.title); text.setText(bse.getBreadCrumbTitle()); text.setTag(bse); if (i == 0) { item.findViewById(R.id.left_icon).setVisibility(View.GONE); } mContainer.addView(item); text.setOnClickListener(mOnClickListener); } } int viewI = numEntries + numPreEntries; numViews = mContainer.getChildCount(); while (numViews > viewI) { mContainer.removeViewAt(numViews - 1); numViews--; } for (int i = 0; i < numViews; i++) { final View child = mContainer.getChildAt(i); child.findViewById(R.id.title).setEnabled(i < numViews - 1); if (mMaxVisible > 0) { child.setVisibility(i < numViews - mMaxVisible ? View.GONE : View.VISIBLE); final View leftIcon = child.findViewById(R.id.left_icon); leftIcon.setVisibility(i > numViews - mMaxVisible && i != 0 ? View.VISIBLE : View.GONE); } } }
public void swapToFragmentView(ScreenList newFragment) { Log.i(t, "swapToFragmentView: " + newFragment.toString()); FragmentManager fragmentManager = getSupportFragmentManager(); Fragment fragment = null; if(ScreenList.BATTERY_INFO == newFragment) { fragment = fragmentManager.findFragmentById(BatteryInfoFragment.LAYOUT_ID); if(null == fragment) { fragment = new BatteryInfoFragment(); } } else if(ScreenList.OPTIMIZATION == newFragment) { fragment = fragmentManager.findFragmentById(OptimizationFragment.LAYOUT_ID); if(null == fragment) { fragment = new OptimizationFragment(); } } else if(ScreenList.CHARGE == newFragment) { fragment = fragmentManager.findFragmentById(ChargeFragment.LAYOUT_ID); if(null == fragment) { fragment = new ChargeFragment(); } } else if(ScreenList.CONSUMPTION == newFragment) { fragment = fragmentManager.findFragmentById(ConsumptionFragment.LAYOUT_ID); if(null == fragment) { fragment = new ConsumptionFragment(); } } else if(ScreenList.ABOUT == newFragment) { fragment = fragmentManager.findFragmentById(AboutFragment.LAYOUT_ID); if(null == fragment) { fragment = new AboutFragment(); } } else { throw new IllegalStateException("Unrecognized ScreenList type"); } currentFragment = newFragment; BackStackEntry entry = null; for (int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) { BackStackEntry e = fragmentManager.getBackStackEntryAt(i); if (e.getName().equals(currentFragment.name())) { entry = e; break; } } if(null != entry) { // flush backward, including the screen want to go back to //fragmentManager.popBackStackImmediate(currentFragment.name(), FragmentManager.POP_BACK_STACK_INCLUSIVE); fragmentManager.popBackStack(currentFragment.name(), FragmentManager.POP_BACK_STACK_INCLUSIVE); } // add transaction to show the screen we want FragmentTransaction transaction = fragmentManager.beginTransaction(); transaction.replace(R.id.main_frame, fragment); transaction.addToBackStack(currentFragment.name()); transaction.commit(); levelSafeInvalidateOptionsMenu(); }