/** * Pop the backstack at the given position until the Fragment with the given fragmentTag is on top * * @param stackId the position of the backstack which should be used * @param fragmentTag the fragmentTag which belongs to the Fragment that should be gone back to * @return true if the Fragment with the given fragmentTag is now on top. False if Fragment with * given fragmentTag not found */ public boolean backToFragment(int stackId, String fragmentTag, boolean withBundle) { ArrayList<FragmentStateHolder> fragmentsStack = mMapOfStacks.get(stackId); for (FragmentStateHolder fpb : fragmentsStack) { if (fpb.fragmentTag.equals(fragmentTag)) { if (fragmentsStack.size() > 1) { while (fragmentsStack.size() > 0 && !(fragmentsStack.get(fragmentsStack.size() - 1).fragmentTag.equals(fragmentTag))) { fragmentsStack.remove(fragmentsStack.get(fragmentsStack.size() - 1)); } FragmentTransaction ft = mFragmentManager.beginTransaction(); if (withBundle) { Bundle bundle = new Bundle(); bundle.putLong(fpb.tomahawkListItemType, fpb.tomahawkListItemId); bundle.putInt(TomahawkFragment.TOMAHAWK_LIST_SCROLL_POSITION, fpb.listScrollPosition); bundle.putString(SearchableFragment.SEARCHABLEFRAGMENT_QUERY_STRING, fpb.queryString); ft.replace( mContentFrameId, Fragment.instantiate(mActivity, fpb.clss.getName(), bundle), fpb.fragmentTag); } else { ft.replace( mContentFrameId, Fragment.instantiate(mActivity, fpb.clss.getName()), fpb.fragmentTag); } ft.commit(); mActivity.onBackStackChanged(); } return fragmentsStack.get(fragmentsStack.size() - 1).equals(fragmentTag); } } return false; }
/** * Replaces the current fragment by the previous fragment stored in the backStack. Does nothing * and returns false if no fragment is back-stacked. */ public boolean back(int stackId) { ArrayList<FragmentStateHolder> fragmentsStack = mMapOfStacks.get(stackId); if (fragmentsStack.size() > 1) { FragmentStateHolder previousFragmentStateHolder = fragmentsStack.get(fragmentsStack.size() - 2); // Restore the remembered fragment and remove it from back fragments. this.replace(stackId, previousFragmentStateHolder, true); mActivity.onBackStackChanged(); return true; } // Nothing to go back. return false; }
public void setCurrentStackId(int stackToShow) { if (mCurrentlyShownStack != stackToShow) { mCurrentlyShownStack = stackToShow; ArrayList<FragmentStateHolder> stack = mMapOfStacks.get(stackToShow); FragmentTransaction ft = mFragmentManager.beginTransaction(); FragmentStateHolder fragmentStateHolder = stack.get(stack.size() - 1); Bundle bundle = new Bundle(); bundle.putLong( fragmentStateHolder.tomahawkListItemType, fragmentStateHolder.tomahawkListItemId); bundle.putInt( TomahawkFragment.TOMAHAWK_LIST_SCROLL_POSITION, fragmentStateHolder.listScrollPosition); bundle.putString( SearchableFragment.SEARCHABLEFRAGMENT_QUERY_STRING, fragmentStateHolder.queryString); ft.replace( mContentFrameId, Fragment.instantiate(mActivity, fragmentStateHolder.clss.getName(), bundle), stack.get(stack.size() - 1).fragmentTag); ft.commit(); } mActivity.onBackStackChanged(); }
/** Replaces the view pager fragment at specified position. */ public void replace(int stackId, FragmentStateHolder fragmentStateHolder, boolean isBackAction) { // Get fragmentsStack for the given (tabs)position ArrayList<FragmentStateHolder> fragmentStateHolders = mMapOfStacks.get(stackId); FragmentStateHolder currentFragmentStateHolder = fragmentStateHolders.get(fragmentStateHolders.size() - 1); if (currentFragmentStateHolder != null) { // Replace the fragment using a transaction. FragmentTransaction ft = mFragmentManager.beginTransaction(); if (isBackAction) { fragmentStateHolders.remove(currentFragmentStateHolder); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE); } else { Fragment currentFragment = mFragmentManager.findFragmentByTag(currentFragmentStateHolder.fragmentTag); if (currentFragment != null && currentFragment instanceof TomahawkFragment) { currentFragmentStateHolder.listScrollPosition = ((TomahawkFragment) currentFragment).getListScrollPosition(); } fragmentStateHolders.add(fragmentStateHolder); ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); } Bundle bundle = new Bundle(); bundle.putLong( fragmentStateHolder.tomahawkListItemType, fragmentStateHolder.tomahawkListItemId); bundle.putInt( TomahawkFragment.TOMAHAWK_LIST_SCROLL_POSITION, fragmentStateHolder.listScrollPosition); bundle.putString( SearchableFragment.SEARCHABLEFRAGMENT_QUERY_STRING, fragmentStateHolder.queryString); ft.replace( mContentFrameId, Fragment.instantiate(mActivity, fragmentStateHolder.clss.getName(), bundle), fragmentStateHolder.fragmentTag); ft.commit(); } mActivity.onBackStackChanged(); }