Example usage for android.app FragmentTransaction commit

List of usage examples for android.app FragmentTransaction commit

Introduction

In this page you can find the example usage for android.app FragmentTransaction commit.

Prototype

public abstract int commit();

Source Link

Document

Schedules a commit of this transaction.

Usage

From source file:com.android.calendar.AllInOneActivity.java

private void setMainPane(FragmentTransaction ft, int viewId, int viewType, long timeMillis, boolean force) {
    if (mOnSaveInstanceStateCalled) {
        return;//from w  ww .ja  v a  2 s .c  om
    }
    if (!force && mCurrentView == viewType) {
        return;
    }

    // Remove this when transition to and from month view looks fine.
    boolean doTransition = viewType != ViewType.MONTH && mCurrentView != ViewType.MONTH;
    FragmentManager fragmentManager = getFragmentManager();
    // Check if our previous view was an Agenda view
    // TODO remove this if framework ever supports nested fragments
    if (mCurrentView == ViewType.AGENDA) {
        // If it was, we need to do some cleanup on it to prevent the
        // edit/delete buttons from coming back on a rotation.
        Fragment oldFrag = fragmentManager.findFragmentById(viewId);
        if (oldFrag instanceof AgendaFragment) {
            ((AgendaFragment) oldFrag).removeFragments(fragmentManager);
        }
    }

    if (viewType != mCurrentView) {
        // The rules for this previous view are different than the
        // controller's and are used for intercepting the back button.
        if (mCurrentView != ViewType.EDIT && mCurrentView > 0) {
            mPreviousView = mCurrentView;
        }
        mCurrentView = viewType;
    }
    // Create new fragment
    Fragment frag = null;
    Fragment secFrag = null;
    switch (viewType) {
    case ViewType.AGENDA:
        mNavigationView.getMenu().findItem(R.id.agenda_menu_item).setChecked(true);
        frag = new AgendaFragment(timeMillis, false);
        if (mIsTabletConfig) {
            mToolbar.setTitle(R.string.agenda_view);
        }
        break;
    case ViewType.DAY:
        mNavigationView.getMenu().findItem(R.id.day_menu_item).setChecked(true);
        frag = new DayFragment(timeMillis, 1);
        if (mIsTabletConfig) {
            mToolbar.setTitle(R.string.day_view);
        }
        break;
    case ViewType.MONTH:
        mNavigationView.getMenu().findItem(R.id.month_menu_item).setChecked(true);
        frag = new MonthByWeekFragment(timeMillis, false);
        if (mShowAgendaWithMonth) {
            secFrag = new AgendaFragment(timeMillis, false);
        }
        if (mIsTabletConfig) {
            mToolbar.setTitle(R.string.month_view);
        }
        break;
    case ViewType.WEEK:
    default:
        mNavigationView.getMenu().findItem(R.id.week_menu_item).setChecked(true);
        frag = new DayFragment(timeMillis, Utils.getDaysPerWeek(this));
        if (mIsTabletConfig) {
            mToolbar.setTitle(R.string.week_view);
        }
        break;
    }
    // Update the current view so that the menu can update its look according to the
    // current view.
    if (mCalendarToolbarHandler != null) {
        mCalendarToolbarHandler.setCurrentMainView(viewType);
    }

    if (!mIsTabletConfig) {
        refreshActionbarTitle(timeMillis);
    }

    // Show date only on tablet configurations in views different than Agenda
    if (!mIsTabletConfig) {
        mDateRange.setVisibility(View.GONE);
    } else if (viewType != ViewType.AGENDA) {
        mDateRange.setVisibility(View.VISIBLE);
    } else {
        mDateRange.setVisibility(View.GONE);
    }

    // Clear unnecessary buttons from the option menu when switching from the agenda view
    if (viewType != ViewType.AGENDA) {
        clearOptionsMenu();
    }

    boolean doCommit = false;
    if (ft == null) {
        doCommit = true;
        ft = fragmentManager.beginTransaction();
    }

    if (doTransition) {
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    }

    ft.replace(viewId, frag);
    if (mShowAgendaWithMonth) {

        // Show/hide secondary fragment

        if (secFrag != null) {
            ft.replace(R.id.secondary_pane, secFrag);
            mSecondaryPane.setVisibility(View.VISIBLE);
        } else {
            mSecondaryPane.setVisibility(View.GONE);
            Fragment f = fragmentManager.findFragmentById(R.id.secondary_pane);
            if (f != null) {
                ft.remove(f);
            }
            mController.deregisterEventHandler(R.id.secondary_pane);
        }
    }
    if (DEBUG) {
        Log.d(TAG, "Adding handler with viewId " + viewId + " and type " + viewType);
    }
    // If the key is already registered this will replace it
    mController.registerEventHandler(viewId, (EventHandler) frag);
    if (secFrag != null) {
        mController.registerEventHandler(viewId, (EventHandler) secFrag);
    }

    if (doCommit) {
        if (DEBUG) {
            Log.d(TAG, "setMainPane AllInOne=" + this + " finishing:" + this.isFinishing());
        }
        ft.commit();
    }
}

From source file:de.anderdonau.spacetrader.Main.java

public void changeFragment(FRAGMENTS fragment) {
    hide_keyboard(this);
    if (fragment == gameState.currentState && currentFragment != null) {
        // Sometimes this seems to be unset. Try to workaround it.
        currentFragment.gameState = gameState;
        if (currentFragment.update()) {
            return;
        }/*from ww  w  .  j a v a2 s . c o m*/
    }
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    if (fragment == FRAGMENTS.NEW_GAME || fragment == FRAGMENTS.ENCOUNTER) {
        transaction.hide(mNavigationDrawerFragment);
    } else {
        transaction.show(mNavigationDrawerFragment);
    }

    Bundle args = new Bundle();
    args.putSerializable("gamestate", gameState);

    switch (fragment) {
    case AVERAGE_PRICES:
        currentFragment = new FragmentAveragePrices();
        break;
    case BANK:
        currentFragment = new FragmentBank();
        break;
    case BUY_CARGO:
        currentFragment = new FragmentBuyCargo();
        break;
    case BUY_EQUIPMENT:
        currentFragment = new FragmentBuyEquipment();
        break;
    case BUY_NEW_SHIP:
        currentFragment = new FragmentBuyNewShip();
        break;
    case COMMANDER_STATUS:
        currentFragment = new FragmentCommanderStatus();
        break;
    case DUMP:
        currentFragment = new FragmentDumpCargo();
        break;
    case ENCOUNTER:
        currentFragment = new FragmentEncounter();
        break;
    case GALACTIC_CHART:
        currentFragment = new FragmentGalacticChart();
        break;
    case NEW_GAME:
        currentFragment = new FragmentStartNewGame();
        break;
    case OPTIONS:
        currentFragment = new FragmentOptions();
        break;
    case PERSONNEL_ROSTER:
        currentFragment = new FragmentPersonnelRoster();
        break;
    case PLUNDER:
        currentFragment = new FragmentPlunderCargo();
        break;
    case SELL_CARGO:
        currentFragment = new FragmentSellCargo();
        break;
    case SELL_EQUIPMENT:
        currentFragment = new FragmentSellEquipment();
        break;
    case SHIPYARD:
        currentFragment = new FragmentShipyard();
        break;
    case SHIP_INFO:
        currentFragment = new FragmentShipInfo();
        break;
    case SHORTCUTS:
        currentFragment = new FragmentShortcuts();
        break;
    case SHORT_RANGE_CHART:
        currentFragment = new FragmentShortRangeChart();
        break;
    case SYSTEM_INFORMATION:
        currentFragment = new FragmentSystemInformation();
        break;
    case VERY_RARE_CHEAT:
        currentFragment = new FragmentVeryRare();
        break;
    case WARP_SYSTEM_INFORMATION:
        currentFragment = new FragmentWarpSystemInformation();
        break;
    default:
        return;
    }

    currentFragment.setArguments(args);
    transaction.replace(R.id.container, currentFragment);
    transaction.commit();
    gameState.currentState = fragment;

    invalidateOptionsMenu();
}