Example usage for android.support.v4.app FragmentManager popBackStackImmediate

List of usage examples for android.support.v4.app FragmentManager popBackStackImmediate

Introduction

In this page you can find the example usage for android.support.v4.app FragmentManager popBackStackImmediate.

Prototype

public abstract boolean popBackStackImmediate();

Source Link

Document

Like #popBackStack() , but performs the operation immediately inside of the call.

Usage

From source file:com.dwdesign.tweetings.activity.HomeActivity.java

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    switch (item.getItemId()) {
    case MENU_HOME: {
        final FragmentManager fm = getSupportFragmentManager();
        if (isDualPaneMode() && !FragmentManagerTrojan.isStateSaved(fm)) {
            final int count = fm.getBackStackEntryCount();
            for (int i = 0; i < count; i++) {
                fm.popBackStackImmediate();
            }// w  w w .j av  a 2 s  . c o m
            setSupportProgressBarIndeterminateVisibility(false);
        }
        break;
    }
    case MENU_COMPOSE: {
        if (mComposeButton != null) {
            onClick(mComposeButton);
        }
        break;
    }
    case MENU_SEARCH: {
        onSearchRequested();
        break;
    }
    case MENU_SELECT_ACCOUNT: {
        startActivityForResult(new Intent(INTENT_ACTION_SELECT_ACCOUNT), REQUEST_SELECT_ACCOUNT);
        break;
    }
    case MENU_SETTINGS: {
        startActivity(new Intent(INTENT_ACTION_SETTINGS));
        break;
    }
    }
    return super.onOptionsItemSelected(item);
}

From source file:org.solovyev.android.messenger.BaseFragmentActivity.java

private void initMainPaneFragment() {
    final FragmentManager fm = getSupportFragmentManager();
    final MessengerMultiPaneFragmentManager multiPaneFragmentManager = getMultiPaneFragmentManager();

    final Fragment mainFragment = fm.findFragmentById(R.id.content_first_pane);
    if (mainFragment != null) {
        final PrimaryFragment primaryFragment = getPrimaryFragment(mainFragment);
        if (primaryFragment != null) {

            final Stack<MultiPaneFragmentDef> fragmentDefs = new Stack<MultiPaneFragmentDef>();

            while (true) {
                final Fragment secondFragment = fm.findFragmentById(R.id.content_second_pane);
                if (secondFragment != null) {
                    // fragment should be copied before popping back stack
                    final MultiPaneFragmentDef fragmentDef = newCopyingFragmentDef(secondFragment, true);
                    if (!primaryFragment.isAddToBackStack()) {
                        if (fm.popBackStackImmediate()) {
                            tryPushSecondFragment(fragmentDefs, fragmentDef, secondFragment);
                        } else {
                            tryPushSecondFragment(fragmentDefs, fragmentDef, secondFragment);
                            // nothing to pop => stop
                            break;
                        }//from   ww w .j  a v  a 2 s.  c o m
                    } else {
                        // primary fragment itself is on back stack => we cannot pop back stack as we can pop it.
                        // let's just add latest fragment on the second pane and put it on on the stack
                        tryPushSecondFragment(fragmentDefs, fragmentDef, secondFragment);
                        break;
                    }
                } else {
                    // no second fragment => stop
                    break;
                }
            }

            while (!fragmentDefs.isEmpty()) {
                multiPaneFragmentManager.setMainFragment(fragmentDefs.pop());
            }
        }
    }
}

From source file:org.solovyev.android.messenger.BaseFragmentActivity.java

private void initDualPaneFragments() {
    final FragmentManager fm = getSupportFragmentManager();

    // We need to be sure that the fragment which is shown on the main pane is primary.
    // If it's not a primary fragment we need to move it to the secondary pane.
    // To restore the primary fragment we just pop back stack (as primary fragment must be somewhere in back stack)
    // As we need to restore second fragment's state let's copy arguments and instance state and pass it to the newly created argument.

    final Stack<MultiPaneFragmentDef> fragmentDefs = new Stack<MultiPaneFragmentDef>();

    while (true) {
        final Fragment mainFragment = fm.findFragmentById(R.id.content_first_pane);
        if (mainFragment != null) {
            if (!isPrimaryFragment(mainFragment)) {
                // NOTE: we must save local copies before popping the back stack as these values might change
                final MultiPaneFragmentDef fragmentDef = newCopyingFragmentDef(mainFragment, false);

                if (fm.popBackStackImmediate()) {
                    fragmentDefs.push(fragmentDef);
                } else {
                    // nothing to pop => stop
                    if (fragmentDefs.isEmpty()) {
                        final ActionBar.Tab selectedTab = getSupportActionBar().getSelectedTab();
                        if (selectedTab != null) {
                            selectedTab.select();
                        }//from www.  j a  va  2  s .com
                    }
                    break;
                }
            } else {
                // primary fragment => stop
                break;
            }
        } else {
            // main pane empty => stop
            break;
        }
    }

    boolean first = true;
    while (!fragmentDefs.isEmpty()) {
        MultiPaneFragmentDef fragmentDef = fragmentDefs.pop();
        if (first) {
            first = false;
            fragmentDef = copy(fragmentDef, false);
        } else {
            fragmentDef = copy(fragmentDef, true);
        }

        getMultiPaneFragmentManager().setSecondFragment(fragmentDef);
    }
}

From source file:it.iziozi.iziozi.gui.IOBoardActivity.java

private void setupNavButtons() {

    LinearLayout centerNavigationLayout = (LinearLayout) findViewById(R.id.centerLayoutNavigationContainer);
    LinearLayout leftNavigationLayout = (LinearLayout) findViewById(R.id.leftLayoutNavigationContainer);
    LinearLayout rightNavigationLayout = (LinearLayout) findViewById(R.id.rightLayoutNavigationContainer);

    mCenterBackButton = new IconTextView(this);

    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
            (int) getResources().getDimension(R.dimen.navigation_bar_button_size),
            (int) getResources().getDimension(R.dimen.navigation_bar_button_size));
    mCenterBackButton.setLayoutParams(params);
    mCenterBackButton.setGravity(Gravity.CENTER);
    mCenterBackButton.setBackground(getResources().getDrawable(R.drawable.circular_shape));
    mCenterBackButton.setTextColor(Color.WHITE);

    mCenterBackButton.setTextSize(32);//from w  w  w  . j av  a  2  s  .c  om
    Iconify.setIcon(mCenterBackButton, Iconify.IconValue.fa_arrow_left);

    centerNavigationLayout.addView(mCenterBackButton);

    mCenterBackButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("test", "back stack size " + getSupportFragmentManager().getBackStackEntryCount());
            if (getSupportFragmentManager().getBackStackEntryCount() > 0)
                getSupportFragmentManager().popBackStackImmediate();
            refreshView();
        }
    });

    mCenterHomeButton = new IconTextView(this);

    params.setMargins((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20,
            getResources().getDisplayMetrics()), 0, 0, 0);
    mCenterHomeButton.setLayoutParams(params);
    mCenterHomeButton.setGravity(Gravity.CENTER);
    mCenterHomeButton.setTextSize(32);
    mCenterHomeButton.setBackground(getResources().getDrawable(R.drawable.circular_shape));
    mCenterHomeButton.setTextColor(Color.WHITE);

    Iconify.setIcon(mCenterHomeButton, Iconify.IconValue.fa_home);

    centerNavigationLayout.addView(mCenterHomeButton);

    mCenterHomeButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            FragmentManager fm = getSupportFragmentManager();
            while (fm.getBackStackEntryCount() > 0)
                fm.popBackStackImmediate();

            refreshView();

        }
    });

    mLeftNavigationButton = new IconTextView(this);

    mLeftNavigationButton.setLayoutParams(params);
    mLeftNavigationButton.setGravity(Gravity.CENTER);
    mLeftNavigationButton.setBackground(getResources().getDrawable(R.drawable.circular_shape));
    mLeftNavigationButton.setTextColor(Color.WHITE);

    mLeftNavigationButton.setTextSize(32);
    Iconify.setIcon(mLeftNavigationButton, Iconify.IconValue.fa_arrow_left);

    leftNavigationLayout.addView(mLeftNavigationButton);

    mLeftNavigationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (IOGlobalConfiguration.isEditing) {
                mActualLevel.addInnerBoardAtIndex(new IOBoard(), mActualIndex);
                refreshView();
            } else
                paginateLeft();
        }
    });

    mRightNavigationButton = new IconTextView(this);

    mRightNavigationButton.setLayoutParams(params);
    mRightNavigationButton.setGravity(Gravity.CENTER);
    mRightNavigationButton.setBackground(getResources().getDrawable(R.drawable.circular_shape));
    mRightNavigationButton.setTextColor(Color.WHITE);

    mRightNavigationButton.setTextSize(32);
    Iconify.setIcon(mRightNavigationButton, Iconify.IconValue.fa_arrow_right);

    rightNavigationLayout.addView(mRightNavigationButton);

    mRightNavigationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (IOGlobalConfiguration.isEditing) {
                mActualLevel.addInnerBoardAtIndex(new IOBoard(), mActualIndex + 1);

                refreshView(mActualLevel.getActiveIndex() + 1);
            } else {
                paginateRight();
            }
        }
    });

    mCenterTrashNavigationButton = new IconTextView(this);

    mCenterTrashNavigationButton.setLayoutParams(params);
    mCenterTrashNavigationButton.setGravity(Gravity.CENTER);
    mCenterTrashNavigationButton.setBackground(getResources().getDrawable(R.drawable.circular_shape));
    mCenterTrashNavigationButton.setTextColor(Color.WHITE);

    mCenterTrashNavigationButton.setTextSize(32);
    Iconify.setIcon(mCenterTrashNavigationButton, Iconify.IconValue.fa_trash_o);

    centerNavigationLayout.addView(mCenterTrashNavigationButton);

    mCenterTrashNavigationButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            new AlertDialog.Builder(IOBoardActivity.this).setTitle(getResources().getString(R.string.warning))
                    .setMessage(getString(R.string.delete_page_alert))
                    .setPositiveButton(getResources().getString(R.string.yes),
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    mActualLevel.removeBoardAtIndex(mActualIndex);

                                    refreshView();
                                }
                            })
                    .setNegativeButton(getString(R.string.no), null).setCancelable(false).create().show();

        }
    });

    mCenterTrashNavigationButton.setVisibility(View.GONE);
    mCenterHomeButton.setVisibility(View.GONE);
    mCenterBackButton.setVisibility(View.GONE);

}

From source file:com.efithealth.app.activity.MainActivity.java

/**
 * Fragment//from  w ww.ja v  a 2s .  com
 */
private void clearFragment() {
    FragmentManager ft = getSupportFragmentManager();
    int count = ft.getBackStackEntryCount();
    for (int i = 0; i < count; i++) {
        ft.popBackStackImmediate();
        //         Log.i("Fragment popped from stack: ", "just popped: " + i + ".");
    }
    Log.e("MainActivityPopdone:", ft.getFragments().size() + ";" + ft.getBackStackEntryCount());
}

From source file:it.iziozi.iziozi.gui.IOBoardActivity.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.action_settings: {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        LayoutInflater inflater = getLayoutInflater();

        View layoutView = inflater.inflate(R.layout.settings_layout, null);

        Integer rows = mActualLevel.getBoardAtIndex(mActualIndex).getRows();
        Integer columns = mActualLevel.getBoardAtIndex(mActualIndex).getCols();

        final CheckBox bordersCheckbox = (CheckBox) layoutView.findViewById(R.id.bordersCheckbox);

        bordersCheckbox.setChecked(mActiveConfig.getShowBorders());

        builder.setTitle(getResources().getString(R.string.settings)).setView(layoutView).setPositiveButton(
                getResources().getString(R.string.apply), new DialogInterface.OnClickListener() {
                    @Override/*from   w  w  w  . j a va2s.c  om*/
                    public void onClick(DialogInterface dialog, int which) {

                        if (newCols == 0)
                            newCols++;
                        if (newRows == 0)
                            newRows++;

                        mActualLevel.getBoardAtIndex(mActualIndex).setCols(newCols);
                        mActualLevel.getBoardAtIndex(mActualIndex).setRows(newRows);

                        IOBoardActivity.this.mActiveConfig.setShowBorders(bordersCheckbox.isChecked());

                        //TODO:createView();
                        refreshView();
                    }
                }).setNegativeButton(getResources().getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Log.d("dialog", "dismiss and discard");
                            }
                        });

        SeekBar sRows = (SeekBar) layoutView.findViewById(R.id.seekRows);
        SeekBar sCols = (SeekBar) layoutView.findViewById(R.id.seekCols);

        final TextView rowsLbl = (TextView) layoutView.findViewById(R.id.numRowsLbl);
        final TextView colsLbl = (TextView) layoutView.findViewById(R.id.numColsLbl);

        sRows.setProgress(rows);
        sCols.setProgress(columns);

        newRows = rows;
        newCols = columns;

        rowsLbl.setText("" + rows);
        colsLbl.setText("" + columns);

        sRows.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (seekBar.getProgress() == 0)
                    seekBar.setProgress(1);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.d("seeking", "seek rows " + progress);
                newRows = progress;
                rowsLbl.setText("" + progress);
            }
        });

        sCols.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (seekBar.getProgress() == 0)
                    seekBar.setProgress(1);
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                Log.d("seeking", "seek cols " + progress);
                newCols = progress;
                colsLbl.setText("" + progress);
            }
        });

        builder.create().show();
        break;
    }
    case R.id.editMode: {
        Log.d("options menu", "edit mode selected");
        item.setChecked(!item.isChecked());
        toggleEditing();

        break;
    }

    case R.id.scanMode: {
        Log.d("options menu", "scan mode selected");
        item.setChecked(!item.isChecked());
        IOGlobalConfiguration.isScanMode = item.isChecked();

        if (IOGlobalConfiguration.isScanMode)
            startScanMode();
        else
            stopScanMode();

        break;
    }

    case R.id.action_new: {

        new AlertDialog.Builder(this).setTitle(getResources().getString(R.string.warning))
                .setMessage(getString(R.string.new_board_alert))
                .setPositiveButton(getResources().getString(R.string.yes),
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                FragmentManager fm = getSupportFragmentManager();
                                while (fm.getBackStackEntryCount() > 0)
                                    fm.popBackStackImmediate();

                                mActiveConfig = new IOConfiguration();
                                fm.beginTransaction()
                                        .replace(mFrameLayout.getId(),
                                                IOPaginatedBoardFragment.newInstance(mActiveConfig.getLevel()))
                                        .commit();

                                if (!IOGlobalConfiguration.isEditing)
                                    toggleEditing();

                            }
                        })
                .setNegativeButton(getResources().getString(R.string.cancel), null).setCancelable(false)
                .create().show();

        break;
    }

    case R.id.action_save: {
        if (null == mActualConfigName)
            IOBoardActivity.this.mActiveConfig.save();
        else
            IOBoardActivity.this.mActiveConfig.saveAs(mActualConfigName);
        break;
    }

    case R.id.action_save_as: {

        final AlertDialog.Builder alert = new AlertDialog.Builder(this);

        View contentView = getLayoutInflater().inflate(R.layout.rename_alert_layout, null);

        final EditText inputText = (EditText) contentView.findViewById(R.id.newNameEditText);

        alert.setView(contentView);
        alert.setPositiveButton(getString(R.string.confirm), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {

                String value = inputText.getText().toString().trim();

                if (value.indexOf(".xml") != -1)
                    value = value.replace(".xml", "");

                File dirFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
                        IOApplication.APPLICATION_NAME + "/boards");
                File file = new File(dirFile.toString(), value + ".xml");

                if (file.exists()) {
                    dialog.cancel();

                    new AlertDialog.Builder(IOBoardActivity.this).setTitle(getString(R.string.warning))
                            .setMessage(getString(R.string.file_already_exists))
                            .setPositiveButton(getString(R.string.continue_string), null).create().show();

                } else {
                    IOBoardActivity.this.mActiveConfig.saveAs(value);
                    mActualConfigName = value;
                }
            }
        });

        alert.setNegativeButton(getString(R.string.cancel), new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                dialog.cancel();
            }
        });
        alert.show();

        break;
    }

    case R.id.action_load: {

        File dirFile = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
                IOApplication.APPLICATION_NAME + "/boards");
        if (!dirFile.exists())
            dirFile.mkdirs();

        final String[] configFiles = dirFile.list(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String filename) {

                if (filename.indexOf(".xml") != -1)
                    return true;

                return false;
            }
        });

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.select_dialog_item);

        adapter.addAll(configFiles);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle(getResources().getString(R.string.choose))
                .setAdapter(adapter, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Log.d("media_debug", "click on item " + which);

                        String fileName = configFiles[which];
                        Log.d("board_debug", fileName);

                        FragmentManager fm = getSupportFragmentManager();
                        while (fm.getBackStackEntryCount() > 0)
                            fm.popBackStackImmediate();

                        mActiveConfig = IOConfiguration.getSavedConfiguration(fileName);

                        fm.beginTransaction()
                                .replace(mFrameLayout.getId(),
                                        IOPaginatedBoardFragment.newInstance(mActiveConfig.getLevel()))
                                .commit();

                    }
                }).setNegativeButton(getResources().getString(R.string.cancel), null).create().show();

        break;
    }

    case R.id.action_about: {
        Intent aboutIntent = new Intent(getApplicationContext(), IOAboutActivity.class);
        startActivity(aboutIntent);
        break;
    }

    case R.id.action_exit: {
        finish();
        break;
    }

    case R.id.action_lock: {
        if (IOGlobalConfiguration.isEditing)
            toggleEditing();
        lockUI();
        break;
    }

    default:
        break;

    }

    return super.onOptionsItemSelected(item);
}

From source file:com.jefftharris.passwdsafe.PasswdSafe.java

/**
 * Check whether to confirm before performing a navigation change
 *//*from   w w  w. j a  v a2 s .c  o m*/
private void checkNavigation(final boolean popOnConfirm, final Runnable navRun) {
    boolean doPrompt = false;
    switch (itsCurrViewMode) {
    case EDIT_RECORD: {
        doPrompt = true;
        break;
    }
    case INIT:
    case FILE_OPEN:
    case FILE_NEW:
    case VIEW_LIST:
    case VIEW_RECORD:
    case CHANGING_PASSWORD:
    case VIEW_ABOUT:
    case VIEW_EXPIRATION:
    case VIEW_POLICY_LIST:
    case VIEW_PREFERENCES: {
        break;
    }
    }

    if (doPrompt) {
        DialogInterface.OnClickListener continueListener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                if (popOnConfirm) {
                    FragmentManager fragMgr = getSupportFragmentManager();
                    fragMgr.popBackStackImmediate();
                }
                navRun.run();
            }
        };
        new AlertDialog.Builder(this).setTitle(R.string.continue_p)
                .setMessage(R.string.any_changes_will_be_lost)
                .setPositiveButton(R.string.continue_str, continueListener)
                .setNegativeButton(R.string.cancel, null).show();
    } else {
        navRun.run();
    }
}

From source file:com.jefftharris.passwdsafe.PasswdSafe.java

/**
 * Finish editing the file// ww  w.  j  a v a  2s  .  co m
 */
private void finishEdit(final boolean save, final boolean popBack, final String popTag,
        final PasswdLocation newLocation, final Runnable postSaveRun) {
    Runnable saveRun = new Runnable() {
        @Override
        public void run() {
            if (save) {
                itsFileDataFrag.refreshFileData();
            }
            boolean resetLoc = shouldResetLoc();

            if (popBack) {
                FragmentManager fragMgr = getSupportFragmentManager();
                fragMgr.popBackStackImmediate();

                if (popTag != null) {
                    //noinspection StatementWithEmptyBody
                    while (fragMgr.popBackStackImmediate(popTag, FragmentManager.POP_BACK_STACK_INCLUSIVE)) {
                        // Pop all fragments up to the first use of the
                        // given tag
                    }
                }
            }

            if (resetLoc) {
                changeOpenView(new PasswdLocation(), true);
            }

            if (postSaveRun != null) {
                postSaveRun.run();
            }
        }

        /**
         * Should the location be reset
         */
        private boolean shouldResetLoc() {
            if (!save || (newLocation == null)) {
                return false;
            }

            if (!newLocation.equalGroups(itsLocation)) {
                return true;
            }

            PasswdFileDataView dataView = itsFileDataFrag.getFileDataView();
            return !dataView.hasGroup(newLocation.getRecordGroup());
        }
    };

    if (save) {
        final ObjectHolder<String> fileId = new ObjectHolder<>("");
        itsFileDataFrag.useFileData(new PasswdFileDataUser() {
            @Override
            public void useFileData(@NonNull PasswdFileData fileData) {
                fileId.set(fileData.getUri().getIdentifier(PasswdSafe.this, false));
            }
        });
        itsCurrTask = new SaveTask(fileId.get(), saveRun, this);
        itsCurrTask.execute();
    } else {
        saveRun.run();
    }
}

From source file:com.gelakinetic.mtgfam.FamiliarActivity.java

/**
 * Select an item from the drawer menu. This will highlight the entry and manage fragment transactions.
 *
 * @param resId The string resource ID of the entry
 *///  w  ww . j a v a2  s  . c o m
private void selectItem(int resId, Bundle args, boolean shouldClearFragmentStack, boolean forceSelect) {

    int position = 0;
    for (DrawerEntry entry : mPageEntries) {
        if (resId == entry.mNameResource) {
            break;
        }
        position++;
    }

    mCurrentFrag = position;
    Fragment newFrag;
    /* Pick the new fragment */
    switch (resId) {
    case R.string.main_card_search: {
        /* If this is a quick search intent, launch either the card view or result list directly */
        if (args != null && args.containsKey(CardViewPagerFragment.CARD_ID_ARRAY)) {
            newFrag = new CardViewPagerFragment();
        } else if (args != null && args.containsKey(SearchViewFragment.CRITERIA)) {
            newFrag = new ResultListFragment();
        } else {
            newFrag = new SearchViewFragment();
        }
        break;
    }
    case R.string.main_life_counter: {
        newFrag = new LifeCounterFragment();
        break;
    }
    case R.string.main_mana_pool: {
        newFrag = new ManaPoolFragment();
        break;
    }
    case R.string.main_dice: {
        newFrag = new DiceFragment();
        break;
    }
    case R.string.main_trade: {
        newFrag = new TradeFragment();
        break;
    }
    case R.string.main_wishlist: {
        newFrag = new WishlistFragment();
        break;
    }
    case R.string.main_decklist: {
        newFrag = new DecklistFragment();
        break;
    }
    case R.string.main_timer: {
        newFrag = new RoundTimerFragment();
        break;
    }
    case R.string.main_rules: {
        newFrag = new RulesFragment();
        break;
    }
    case R.string.main_judges_corner: {
        newFrag = new JudgesCornerFragment();
        break;
    }
    case R.string.main_mojhosto: {
        newFrag = new MoJhoStoFragment();
        break;
    }
    case R.string.main_profile: {
        newFrag = new ProfileFragment();
        break;
    }
    default:
        return;
    }

    try {
        if (!forceSelect && ((Object) newFrag).getClass().equals(
                ((Object) getSupportFragmentManager().findFragmentById(R.id.fragment_container)).getClass())) {
            /* This is the same fragment, just close the menu */
            mDrawerLayout.closeDrawer(mDrawerList);
            return;
        }
    } catch (NullPointerException e) {
        /* no fragment to compare to */
    }

    if (args != null) {
        newFrag.setArguments(args);
    }

    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft;
    if (fm != null) {
        if (shouldClearFragmentStack) {
            /* Remove any current fragments on the back stack */
            while (fm.getBackStackEntryCount() > 0) {
                fm.popBackStackImmediate();
            }
        }

        /* Begin a new transaction */
        ft = fm.beginTransaction();

        /* Replace or add the fragment */
        ft.replace(R.id.fragment_container, newFrag, FamiliarActivity.FRAGMENT_TAG);
        if (!shouldClearFragmentStack) {
            ft.addToBackStack(null);
        }
        ft.commit();

        /* Color the icon when the fragment changes */
        View drawerListItemView = mDrawerList.getChildAt(position);
        if (drawerListItemView != null) {
            TextView textView = (TextView) drawerListItemView.findViewById(R.id.drawer_entry_name);
            if (textView != null) {
                mPagesAdapter.colorDrawerEntry(textView);
            }
        }
    }
}

From source file:com.jefftharris.passwdsafe.PasswdSafe.java

/**
 * Change the view of the activity with an optional identifying tag for the
 * transition//from  ww w . j a va2s.co  m
 */
private void doChangeView(final ChangeMode mode, final Fragment contentFrag, final String transTag) {
    checkNavigation(true, new Runnable() {
        public void run() {
            FragmentManager fragMgr = getSupportFragmentManager();
            FragmentTransaction txn = fragMgr.beginTransaction();

            boolean clearBackStack = false;
            boolean supportsBack = false;
            switch (mode) {
            case INIT:
            case FILE_OPEN:
            case FILE_NEW:
            case OPEN_INIT: {
                clearBackStack = true;
                break;
            }
            case OPEN:
            case RECORD:
            case EDIT_RECORD:
            case CHANGE_PASSWORD:
            case VIEW_ABOUT:
            case VIEW_EXPIRATION:
            case VIEW_POLICY_LIST:
            case VIEW_PREFERENCES: {
                supportsBack = true;
                break;
            }
            }
            if (clearBackStack) {
                //noinspection StatementWithEmptyBody
                while (fragMgr.popBackStackImmediate()) {
                    // Clear back stack
                }
            }

            txn.setTransitionStyle(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            if (contentFrag != null) {
                txn.replace(R.id.content, contentFrag);
            } else {
                Fragment currFrag = fragMgr.findFragmentById(R.id.content);
                if ((currFrag != null) && currFrag.isAdded()) {
                    txn.remove(currFrag);
                }
            }

            if (supportsBack) {
                txn.addToBackStack(transTag);
            }

            txn.commit();
            itsIsConfirmBackClosed = true;
        }
    });
}