Java Code Examples for android.support.v4.app.fragmentmanager#popBackStack()

The following examples show how to use android.support.v4.app.fragmentmanager#popBackStack() . 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: NavigatorHelper.java    From Navigator with Apache License 2.0 6 votes vote down vote up
public void goBackTo(String tag) throws NavigatorException {
    FragmentManager fragmentManager = ((FragmentActivity) mContext).getSupportFragmentManager();
    if (fragmentManager.findFragmentByTag(tag) != null) {
        List<FragmentManager.BackStackEntry> fragmentList = fragmentList();
        Collections.reverse(fragmentList);
        for (int i = 0; i < fragmentList.size(); i++) {
            if (!tag.equalsIgnoreCase(fragmentList.get(i).getName())) {
                fragmentManager.popBackStack();
            } else {
                listStep.add(tag);
                return;
            }
        }
    } else {
        Log.e(TAG, "no fragment found");
        String message = "Fragment with TAG[" + tag + "] not found into backstack entry";
        throw new NavigatorException(message);
    }
}
 
Example 2
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 3
Source File: HelloFacebookSampleActivity.java    From FacebookNewsfeedSample-Android with Apache License 2.0 6 votes vote down vote up
private void onFriendPickerDone(FriendPickerFragment fragment) {
    FragmentManager fm = getSupportFragmentManager();
    fm.popBackStack();

    String results = "";

    Collection<GraphUser> selection = fragment.getSelection();
    if (selection != null && selection.size() > 0) {
        ArrayList<String> names = new ArrayList<String>();
        for (GraphUser user : selection) {
            names.add(user.getName());
        }
        results = TextUtils.join(", ", names);
    } else {
        results = getString(R.string.no_friends_selected);
    }

    showAlert(getString(R.string.you_picked), results);
}
 
Example 4
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 5
Source File: DynamicActivity.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onNavigateUp() {
    FragmentManager fm = this.getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 1) {
        fm.popBackStack();
        return true;
    } else {
        finish();
        return true;
    }
}
 
Example 6
Source File: SinglePaneActivity.java    From MongoExplorer with MIT License 5 votes vote down vote up
@Override
public void onBackPressed() {
	FragmentManager fm = getSupportFragmentManager();

	if (fm.getBackStackEntryCount() > 0) {
		fm.popBackStack();
		return;
	}

	super.onBackPressed();
}
 
Example 7
Source File: ShiftLauncherView.java    From shift with Apache License 2.0 5 votes vote down vote up
private void addFragment(FragmentActivity activity, Fragment fragment, String tag, boolean isVisible) {
    FragmentManager manager = activity.getSupportFragmentManager();
    Fragment oldInstance = manager.findFragmentByTag(tag);
    if (oldInstance == null) {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.add(android.R.id.content, fragment, tag);
        if (!isVisible) {
            transaction.hide(fragment);
        } else if (tag.equals(TABS_TAG)) {
            manager.popBackStack(TABS_BACK_STACK, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            transaction.addToBackStack(TABS_BACK_STACK);
        }
        transaction.commit();
    }
}
 
Example 8
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 9
Source File: CheckoutActivity.java    From px-android with MIT License 5 votes vote down vote up
@Override
public void onBackPressed() {
    final FragmentManager fragmentManager = getSupportFragmentManager();
    if (fragmentManager != null) {
        final int backStackEntryCount = fragmentManager.getBackStackEntryCount();
        Fragment fragment = fragmentManager.findFragmentByTag(TAG_OFFLINE_METHODS_FRAGMENT);

        if (fragment instanceof BackHandler) {
            final boolean shouldHandleBack = ((BackHandler) fragment).handleBack();
            if (!shouldHandleBack) {
                return;
            }
        }

        fragment = fragmentManager.findFragmentByTag(CardFormWithFragment.TAG);
        if (fragment != null && fragment.getChildFragmentManager().getBackStackEntryCount() > 0) {
            fragment.getChildFragmentManager().popBackStack();
            return;
        }

        if (backStackEntryCount > 0) {
            fragmentManager.popBackStack();
            return;
        }

        fragment =
            FragmentUtil.getFragmentByTag(fragmentManager, PayButtonFragment.TAG, PayButtonFragment.class);
        if (fragment == null || !((PayButtonFragment) fragment).isExploding()) {
            super.onBackPressed();
        }
    }
}
 
Example 10
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 11
Source File: MainActivity.java    From android with MIT License 5 votes vote down vote up
@Override
public void onBackPressed() {
    final FragmentManager fm = getSupportFragmentManager();
    final Fragment fragment = fm.findFragmentByTag(Const.FragmentTags.MAP_INFO_EXPANDED);
    if (fragment != null) {
        fm.popBackStack();
    } else {
        super.onBackPressed();
    }
}
 
Example 12
Source File: MainActivity.java    From Androzic with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
	// Handle action buttons
	switch (item.getItemId())
	{
		case android.R.id.home:
			FragmentManager fm = getSupportFragmentManager();
			if (fm.getBackStackEntryCount() > 0)
			{
				View v = getCurrentFocus();
				if (v != null)
				{
					// Hide keyboard
					final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
					imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
				}
				fm.popBackStack();
				return true;
			}
			else
			{
				if (mDrawerToggle.onOptionsItemSelected(item))
				{
					return true;
				}
			}
		default:
			return super.onOptionsItemSelected(item);
	}
}
 
Example 13
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 14
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 15
Source File: MainActivity.java    From Readily with MIT License 5 votes vote down vote up
private void startFileListFragment(){
	FragmentManager fragmentManager = getSupportFragmentManager();
	fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
	fragmentManager.beginTransaction().
			replace(R.id.content_layout, new FileListFragment()).
			commit();
}
 
Example 16
Source File: MainActivity.java    From Cangol-appcore with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onNavigateUp() {
    FragmentManager fm = this.getSupportFragmentManager();
    if(fm.getBackStackEntryCount()>1){
        fm.popBackStack();
        return true;
    }else{
        super.onBackPressed();
        return true;
    }
}
 
Example 17
Source File: HomeActivity.java    From SEAL-Demo with MIT License 4 votes vote down vote up
/**
 * This method allows to clear backStack
 */
public void clearFragmentBackStack() {
    // create a FragmentManager
    FragmentManager fm = getSupportFragmentManager();
    fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
 
Example 18
Source File: BaseLoginState.java    From state-machine-android with MIT License 4 votes vote down vote up
protected void popFragment() {
    FragmentManager fragmentManager = getProvider().provideFragmentManager();
    fragmentManager.popBackStack();
}
 
Example 19
Source File: NavigatorHelper.java    From Navigator with Apache License 2.0 4 votes vote down vote up
public void goBack() throws NavigatorException {
    FragmentManager fragmentManager = ((FragmentActivity) mContext).getSupportFragmentManager();
    if (canGoBack(fragmentManager)) {
        fragmentManager.popBackStack();
    }
}
 
Example 20
Source File: MvcActivity.java    From AndroidMvc with Apache License 2.0 4 votes vote down vote up
private void performBackNav(final NavigationManager.Event.OnLocationBack event) {
    FragmentManager fm = getChildFragmentManager();

    NavLocation lastLoc = event.getLastValue();
    if (lastLoc != null) {
        String lastFragTag = getFragmentTag(lastLoc.getLocationId());
        final MvcFragment lastFrag = (MvcFragment) fm.findFragmentByTag(lastFragTag);

        if (lastFrag != null) {
            lastFrag.onPopAway();
        }
    }

    NavLocation currentLoc = event.getCurrentValue();
    if (currentLoc == null) {
        MvcActivity act = (MvcActivity) getActivity();
        act.actionsOnDestroy.add(new Runnable() {
            @Override
            public void run() {
                destroyNavigator(event.getNavigator());
                pendingNavActions.remove(this);
            }
        });

        MvcActivity mvcActivity = ((MvcActivity) getActivity());
        //Back to null which should finish the current activity
        mvcActivity.performSuperBackKeyPressed();
        mvcActivity.toPrintAppExitMessage = true;
    } else {
        String currentFragTag = getFragmentTag(currentLoc.getLocationId());
        final MvcFragment currentFrag = (MvcFragment) fm.findFragmentByTag(currentFragTag);
        if (currentFrag != null) {
            traverseFragmentAndSubFragments(currentFrag, new FragmentManipulator() {
                @Override
                public void manipulate(Fragment fragment) {
                    if (fragment != null && fragment instanceof MvcFragment) {
                        final MvcFragment frag = ((MvcFragment) fragment);
                        frag.aboutToPopOut = true;
                        frag.registerOnViewReadyListener(new Runnable() {
                            @Override
                            public void run() {
                                destroyNavigator(event.getNavigator());
                                frag.unregisterOnViewReadyListener(this);
                            }
                        });
                    }
                }
            });
        }

        if (event.isFastRewind()) {
            if (currentLoc.getPreviousLocation() == null) {
                if (fm.getBackStackEntryCount() <= 1) {
                    //Has reached bottom. Does nothing in this case
                    return;
                }

                //Pop fragments to the last
                int stackCount = fm.getBackStackEntryCount();
                int timesNeedToPop = 0;
                for (int i = 0; i < stackCount; i++) {
                    if (currentFragTag.equals(fm.getBackStackEntryAt(i).getName())) {
                        timesNeedToPop++;
                    }
                }

                if (timesNeedToPop > 1) {
                    for (int i = 0; i < stackCount - 1; i++) {
                        fm.popBackStack();
                    }
                    fm.executePendingTransactions();
                } else {
                    fm.popBackStack(currentFragTag, 0);
                }
                logger.trace("Navigation back: Fast rewind to home location {}", currentLoc.getLocationId());
            } else {
                String tag = getFragmentTag(currentLoc.getLocationId());
                fm.popBackStack(tag, 0);
                logger.trace("Navigation back: Fast rewind to given location {}", currentLoc.getLocationId());
            }
        } else {
            fm.popBackStack();
            logger.trace("Navigation back: On step back from {} to location {}",
                    event.getLastValue() != null ? event.getLastValue().getLocationId() : null,
                    currentLoc.getLocationId());
        }
    }
}