Java Code Examples for android.app.FragmentManager#popBackStack()

The following examples show how to use android.app.FragmentManager#popBackStack() . These examples are extracted from open source projects. 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 Project: mirror   File: MirrorActivity.java    License: Apache License 2.0 6 votes vote down vote up
private void hideFragment(final Fragment fragment,
                          final boolean removeParentView,
                          final boolean addedToBackStack,
                          final String tag) {
    final FragmentManager manager = getFragmentManager();
    if (manager.findFragmentByTag(tag) != null) {
        final FragmentTransaction transaction = manager.beginTransaction();
        transaction.remove(fragment);
        transaction.commit();
        if (addedToBackStack) {
            manager.popBackStack();
        }

        final int viewId = getParentId(fragment);
        if (removeParentView && viewId != 0) {
            mContentContainer.removeBorderView(viewId);
        }
    }
}
 
Example 2
public void changeContentFragment(final OpenFragmentCallback callback){

        final FragmentManager manager = getFragmentManager();

        // confirmContentClose shouldn't be both used as boolean and as close-message,
        //  this is a bad pattern
        if(confirmContentClose != null){
            new AlertDialog.Builder(this)
                .setMessage(confirmContentClose)
                .setCancelable(false)
                .setPositiveButton(android.R.string.yes,
                    new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                            callback.onOpen();
                        }
                    })
                .setNegativeButton(android.R.string.no, null)
                .show();
        } else {
            manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            callback.onOpen();
        }

    }
 
Example 3
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            FragmentManager fm = getFragmentManager();
            if (fm.getBackStackEntryCount() > 0) {
                fm.popBackStack();
            }
            return true;
        case R.id.menu_item_help:
            AboutDialog aboutDialog = new AboutDialog(this);
            aboutDialog.show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 4
Source Project: driveimageview   File: MainActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            FragmentManager fm = getFragmentManager();
            if (fm.getBackStackEntryCount() > 0) {
                fm.popBackStack();
            }
            return true;
        case ch.haclyon.driveimageview.example.R.id.menu_item_help:
            AboutDialog aboutDialog = new AboutDialog(this);
            aboutDialog.show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 5
Source Project: LyricHere   File: ActionBarCastActivity.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void onBackPressed() {
    // If the drawer is open, back will close it
    if (mDrawerLayout != null && mDrawerLayout.isDrawerOpen(Gravity.START)) {
        mDrawerLayout.closeDrawers();
        return;
    }
    // Otherwise, it may return to the previous fragment stack
    FragmentManager fragmentManager = getFragmentManager();
    if (fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStack();
    } else {
        // Lastly, it will rely on the system behavior for back
        super.onBackPressed();
    }
}
 
Example 6
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (MenuDoubleTabUtil.shouldIgnoreTap()) {
        return false;
    }
    switch (item.getItemId()) {
        case android.R.id.home:
            FragmentManager fm = getFragmentManager();
            if (fm.getBackStackEntryCount() > 0) {
                try {
                    fm.popBackStack();
                } catch (IllegalStateException e) {
                    Log.w(Config.LOGTAG, "Unable to pop back stack after pressing home button");
                }
                return true;
            }
            break;
        case R.id.action_scan_qr_code:
            UriHandlerActivity.scan(this);
            return true;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 7
private void replaceFragment(@Nullable Fragment fragment, boolean clearBackStack) {
    if (fragment == null || isStateSaved) {
        return;
    }

    Fragment currentFragment = getCurrentFragment();
    FragmentManager manager = getFragmentManager();
    if (clearBackStack) {
        manager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }

    @SuppressLint("CommitTransaction")
    FragmentTransaction transaction = manager.beginTransaction()
            .replace(R.id.ym_container, fragment);
    if (!clearBackStack && currentFragment != null) {
        transaction.addToBackStack(fragment.getTag());
    }
    transaction.commit();
    hideKeyboard();
}
 
Example 8
Source Project: stynico   File: FilePickerActivity.java    License: MIT License 5 votes vote down vote up
@Override
public void onBackPressed() {
    FragmentManager fm = getFragmentManager();

    if (fm.getBackStackEntryCount() > 0) {
        fm.popBackStack();
        mCurrentPath = FileUtils.cutLastSegmentOfPath(mCurrentPath);
        updateTitle();
    } else {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }
}
 
Example 9
Source Project: Pano360   File: FilePickerActivity.java    License: MIT License 5 votes vote down vote up
@Override
public void onBackPressed() {
    FragmentManager fm = getFragmentManager();

    if (fm.getBackStackEntryCount() > 0) {
        fm.popBackStack();
        mCurrentPath = FileUtils.cutLastSegmentOfPath(mCurrentPath);
        updateTitle();
    } else {
        setResult(RESULT_CANCELED);
        super.onBackPressed();
    }
}
 
Example 10
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (currentPage) {
        case REGISTER_IR_PAGE:
        case MANAGE_VIRTUAL_PROFILE_PAGE:
            FragmentManager fm = getFragmentManager();
            fm.popBackStack();
            currentPage--;
            break;
        default:
            finish();

    }
    return true;
}
 
Example 11
Source Project: beaconloc   File: BaseActivity.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            FragmentManager fm = getFragmentManager();
            if (fm.getBackStackEntryCount() > 0) {
                fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            } else {
                finish();
            }
            return true;
        case R.id.action_settings:
            launchSettingsActivity();
            return true;
        case R.id.action_view_on_github:
            launchGitHubPage();
            return true;
        case R.id.action_help:
            launchHelpPage();
            return true;
        case R.id.action_donate:
            launchDonatePage();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 12
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (MenuDoubleTabUtil.shouldIgnoreTap()) {
        return false;
    }
    switch (item.getItemId()) {
        case android.R.id.home:
            FragmentManager fm = getFragmentManager();
            if (fm.getBackStackEntryCount() > 0) {
                try {
                    fm.popBackStack();
                } catch (IllegalStateException e) {
                    Log.w(Config.LOGTAG, "Unable to pop back stack after pressing home button");
                }
                return true;
            }
            break;
        case R.id.action_scan_qr_code:
            UriHandlerActivity.scan(this);
            return true;
        case R.id.action_check_updates:
            if (xmppConnectionService.hasInternetConnection()) {
                openInstallFromUnknownSourcesDialogIfNeeded(true);
            } else {
                ToastCompat.makeText(this, R.string.account_status_no_internet, Toast.LENGTH_LONG).show();
            }
            break;
        case R.id.action_invite_user:
            inviteUser();
            break;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 13
Source Project: Hews   File: SettingsActivity.java    License: MIT License 5 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
        FragmentManager fm = getFragmentManager();
        if (fm.getBackStackEntryCount() > 0) {
            fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        } else {
            finish();
        }
    }
    return super.onOptionsItemSelected(item);
}
 
Example 14
@Override
public void onBackPressed(){
	FragmentManager fm = getFragmentManager();
	if (fm.getBackStackEntryCount() > 0) {
		Log.i("MainActivity", "popping backstack");
		fm.popBackStack();

	} else {
		Log.i("MainActivity", "nothing on backstack, calling super");
		super.onBackPressed();
	}
}
 
Example 15
Source Project: moviedb-android   File: MainActivity.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Pops the last item from the back stack.
 * If searchView is opened it hides it.
 * reAttachMovieFragments re-creates our fragments this is due to a bug in the viewPager
 * restoreMovieDetailsState -> when we press back button we would like to restore our previous (if any)
 * saved state for the current fragment. We use custom backStack since the original doesn't save fragment's state.
 */
@Override
public void onBackPressed() {
    FragmentManager fm = getFragmentManager();

    if (mDrawerLayout.isDrawerOpen(mDrawerList))
        mDrawerLayout.closeDrawer(mDrawerList);
    else {
        if (searchViewItem.isActionViewExpanded())
            searchViewItem.collapseActionView();

        else if (fm.getBackStackEntryCount() > 0) {
            String backStackEntry = fm.getBackStackEntryAt(fm.getBackStackEntryCount() - 1).getName();
            if (backStackEntry.equals("movieList"))
                reAttachMovieFragments = true;

            if (backStackEntry.equals("TVList"))
                reAttachTVFragments = true;

            if (backStackEntry.equals("searchList:1"))
                reAttachMovieFragments = true;

            if (backStackEntry.equals("searchList:2"))
                reAttachTVFragments = true;

            restoreMovieDetailsState = true;
            restoreMovieDetailsAdapterState = false;
            if (orientationChanged)
                restoreMovieDetailsAdapterState = true;

            fm.popBackStack();
        } else {
            super.onBackPressed();
        }
    }

}
 
Example 16
public void topFragment(Class<?> cls, int transition, Bundle args) {
    Fragment frag = Fragment.instantiate(
        this,
        cls.getName(),
        args
    );
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    fragmentManager.beginTransaction()
     .replace(R.id.content_frame, frag)
     .setTransition(transition)
     .commit();
    fragmentManager.executePendingTransactions();
    configureFragment(frag);
}
 
Example 17
public void goBack(BaseActivity activity) {
    FragmentManager fm = activity.getFragmentManager();
    fm.popBackStack();
}
 
Example 18
/**
 * 一つ前のFragmentに戻る.
 */
private void back() {
    FragmentManager fm = getActivity().getFragmentManager();
    fm.popBackStack();
}
 
Example 19
Source Project: Hews   File: CommentsActivity.java    License: MIT License 4 votes vote down vote up
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            if (mIsInReplyMode) {
                showDiscardReplyDialog();
                return true;
            }
            FragmentManager fm = getFragmentManager();
            if (fm.getBackStackEntryCount() > 0) {
                fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
            } else {
                finish();
            }
            break;

        case R.id.action_open_post:
            if (mUrl != null) {
                if (mChromeCustomTabsHelper != null) {
                    // build CustomTabs UI
                    CustomTabsIntent.Builder intentBuilder = new CustomTabsIntent.Builder();
                    Utils.setupIntentBuilder(intentBuilder, this, prefs);
                    ChromeCustomTabsHelper.openCustomTab(this, intentBuilder.build(),
                        Utils.validateAndParseUri(mUrl, mPostId), null);
                } else {
                    Intent urlIntent = new Intent(Intent.ACTION_VIEW);
                    urlIntent.setData(Utils.validateAndParseUri(mUrl, mPostId));
                    startActivity(urlIntent);
                }
            }
            break;

        case R.id.action_bookmark:
            changeBookmarkState();
            break;

        case R.id.action_upvote:
            vote(mPostId, Constants.VOTE_UP);
            break;

        case R.id.action_reply:
            if (!Utils.isOnline(this)) {
                Utils.showLongToast(this, R.string.no_connection_prompt);
                return false;
            }
            switchReplyMode(true, mPostId);
            break;

        case R.id.action_refresh:
            CommentsFragment commentFragment =
                (CommentsFragment) getFragmentManager().findFragmentByTag(Constants.FRAGMENT_TAG_COMMENT);
            commentFragment.getPresenter().refresh();
            break;

        case R.id.action_share:
            Intent sendIntent = new Intent();
            sendIntent.setAction(Intent.ACTION_SEND);
            String commentUrl = "https://news.ycombinator.com/item?id=" + mPostId;
            sendIntent.putExtra(Intent.EXTRA_TEXT, commentUrl);
            sendIntent.setType("text/plain");
            startActivity(Intent.createChooser(sendIntent, getString(R.string.share_link_to)));
            break;

        case R.id.action_typography:
            if (!mWindow.isWindowShowing()) {
                mWindow.show();
            } else {
                mWindow.dismiss();
            }
            break;
    }
    return super.onOptionsItemSelected(item);
}
 
Example 20
protected void changeFragment(Fragment fragment, String name, boolean isChild) {
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    if (transaction.isAddToBackStackAllowed()) {
        int count = fragmentManager.getBackStackEntryCount();
        if (count > 0) {
            FragmentManager.BackStackEntry entry =
                    fragmentManager.getBackStackEntryAt(count - 1);

            if (entry != null && name.equals(entry.getName())) {
                fragmentManager.popBackStack();
                if (!isChild) {
                    // We just removed it's duplicate from the back stack
                    // And we want at least one in it
                    transaction.addToBackStack(name);
                }
            }
        }

        if (isChild) {
            transaction.addToBackStack(name);
        }
    }

    if (getResources().getBoolean(R.bool.hide_bottom_bar_on_second_level_views)) {
        if (isChild) {
            if (!isTablet()) {
                hideTabBar();
            }
        } else {
            showTabBar();
        }
    }

    Compatibility.setFragmentTransactionReorderingAllowed(transaction, false);
    if (isChild && isTablet()) {
        transaction.replace(R.id.fragmentContainer2, fragment, name);
        findViewById(R.id.fragmentContainer2).setVisibility(View.VISIBLE);
    } else {
        transaction.replace(R.id.fragmentContainer, fragment, name);
    }
    transaction.commitAllowingStateLoss();
    fragmentManager.executePendingTransactions();
}