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

The following examples show how to use android.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: ConversationsActivity.java    From Conversations with GNU General Public License v3.0 7 votes vote down vote up
@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 2
Source File: PaymentActivity.java    From yandex-money-sdk-android with MIT License 6 votes vote down vote up
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 3
Source File: MirrorActivity.java    From mirror with 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 4
Source File: WorldActivity.java    From blocktopograph with GNU Affero General Public License v3.0 6 votes vote down vote up
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 5
Source File: DriveImageViewActivity.java    From UltimateAndroid with 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 R.id.menu_item_help:
            AboutDialog aboutDialog = new AboutDialog(this);
            aboutDialog.show();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
 
Example 6
Source File: ActionBarCastActivity.java    From LyricHere with 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 7
Source File: MainActivity.java    From driveimageview with 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 8
Source File: FilePickerActivity.java    From stynico with 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 File: EnvelopesActivity.java    From budget-envelopes with GNU General Public License v3.0 5 votes vote down vote up
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 10
Source File: MainActivity.java    From moviedb-android with 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 11
Source File: MainActivity.java    From android-kernel-tweaker with GNU General Public License v3.0 5 votes vote down vote up
@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 12
Source File: SettingsActivity.java    From Hews with 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 13
Source File: ConversationsActivity.java    From Pix-Art-Messenger with GNU General Public License v3.0 5 votes vote down vote up
@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 14
Source File: BaseActivity.java    From beaconloc with 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 15
Source File: IRKitVirtualDeviceListActivity.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@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 16
Source File: FilePickerActivity.java    From Pano360 with 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 17
Source File: CommentsActivity.java    From Hews with 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 18
Source File: MainActivity.java    From linphone-android with GNU General Public License v3.0 4 votes vote down vote up
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();
}
 
Example 19
Source File: AccessTokenDescriptionFragment.java    From DeviceConnect-Android with MIT License 4 votes vote down vote up
/**
 * 一つ前のFragmentに戻る.
 */
private void back() {
    FragmentManager fm = getActivity().getFragmentManager();
    fm.popBackStack();
}
 
Example 20
Source File: Navigator.java    From android-showcase-template with Apache License 2.0 4 votes vote down vote up
public void goBack(BaseActivity activity) {
    FragmentManager fm = activity.getFragmentManager();
    fm.popBackStack();
}