Example usage for android.app FragmentTransaction add

List of usage examples for android.app FragmentTransaction add

Introduction

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

Prototype

public abstract FragmentTransaction add(@IdRes int containerViewId, Fragment fragment, String tag);

Source Link

Document

Add a fragment to the activity state.

Usage

From source file:com.example.office.ui.Office365DemoActivity.java

/**
 * Choose one of the available screens to display (via appropriate Fragment).
 *
 * @param newScreen Screen to be shown./*from www.  jav a 2s  .  c o  m*/
 */
private void switchScreen(UI.Screen newScreen) {
    try {
        ActionBar actionBar = getActionBar();

        mDrawerList.setItemChecked(newScreen.ordinal(), true);
        setTitle(newScreen.getName(this));
        actionBar.setLogo(newScreen.getIcon(this));

        if (newScreen.in(ScreenGroup.MAIL)) {
            Screen currentScreen = Screen.getByTag(mCurrentFragmentTag, this);
            if (!currentScreen.in(ScreenGroup.MAIL)) {
                Fragment newFragment;
                FragmentManager fragmentManager = getFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                switch (newScreen) {
                case CONTACTS:
                    newFragment = new ContactsFragment();
                    break;
                case MAILBOX:
                    newFragment = new DraftsFragment();
                    break;
                case CALENDAR: {
                    newFragment = new CalendarFragment();
                    break;
                }
                default: {
                    newFragment = new DraftsFragment();
                    break;
                }
                }
                fragmentTransaction.add(R.id.content_pane, newFragment, newScreen.getName(this));
                fragmentTransaction.commit();
            }
            actionBar.selectTab(actionBar.getTabAt(newScreen.ordinal()));
            mCurrentFragmentTag = newScreen.getName(this);
        }
        mDrawerLayout.closeDrawer(mDrawerList);
    } catch (Exception e) {
        Logger.logApplicationException(e, getClass().getSimpleName() + ".switchBox(): Error.");
    }
}

From source file:com.android.dialer.DialtactsActivity.java

/**
 * Initiates a fragment transaction to show the dialpad fragment. Animations and other visual
 * updates are handled by a callback which is invoked after the dialpad fragment is shown.
 * @see #onDialpadShown//from  w w w. jav  a  2  s .  co m
 */
private void showDialpadFragment(boolean animate) {
    if (mIsDialpadShown || mStateSaved) {
        return;
    }
    mIsDialpadShown = true;

    mListsFragment.setUserVisibleHint(false);

    final FragmentTransaction ft = getFragmentManager().beginTransaction();
    if (mDialpadFragment == null) {
        mDialpadFragment = new DialpadFragment();
        ft.add(R.id.dialtacts_container, mDialpadFragment, TAG_DIALPAD_FRAGMENT);
    } else {
        ft.show(mDialpadFragment);
    }

    mDialpadFragment.setAnimate(animate);
    Logger.logScreenView(ScreenEvent.DIALPAD, this);
    ft.commit();

    if (animate) {
        mFloatingActionButtonController.scaleOut();
    } else {
        mFloatingActionButtonController.setVisible(false);
        maybeEnterSearchUi();
    }
    mActionBarController.onDialpadUp();

    mListsFragment.getView().animate().alpha(0).withLayer();

    //adjust the title, so the user will know where we're at when the activity start/resumes.
    setTitle(R.string.launcherDialpadActivityLabel);
}

From source file:it.scoppelletti.mobilepower.app.FragmentLayoutController.java

/**
 * Ricostruisce la successione della disposizione dei frammenti nei
 * pannelli.//from  w ww  .  java 2  s  .c o  m
 * 
 * @param  fragmentMgr   Gestore dei frammenti.
 * @param  fragmentQueue Frammenti.
 * @return               Identificatore dell’ultimo elemento inserito
 *                       nel back stack.
 */
private int arrangeFragments(FragmentManager fragmentMgr,
        Queue<FragmentLayoutController.FragmentEntry> fragmentQueue) {
    int i;
    int frameCount, tnId, lastTnId;
    FragmentLayoutController.FragmentEntry entry;
    FragmentSupport newFragment, oldFragment;
    FragmentLayoutController.FragmentEntry[] frames;
    FragmentTransaction fragmentTn = null;

    frameCount = 1;
    frames = new FragmentLayoutController.FragmentEntry[myFrameCount];
    Arrays.fill(frames, null);

    lastTnId = -1;
    while (!fragmentQueue.isEmpty()) {
        tnId = -1;
        entry = fragmentQueue.remove();

        try {
            fragmentTn = fragmentMgr.beginTransaction();

            if (frameCount == myFrameCount) {
                // Tutti i pannelli sono occupati:
                // Sposto ogni frammento nel pannello precedente per
                // liberare l'ultimo.
                for (i = 0; i < frameCount; i++) {
                    if (frames[i] == null) {
                        // Inizialmente il primo pannello risulta vuoto
                        // anche se in realta' e' occupato dal frammento
                        // principale (non di dettaglio).
                        continue;
                    }

                    oldFragment = frames[i].getFragment();
                    newFragment = (i > 0) ? oldFragment.cloneFragment() : null;
                    fragmentTn.remove(oldFragment.asFragment());
                    frames[i] = null;

                    if (newFragment != null) {
                        fragmentTn.replace(myFrameIds[i - 1], newFragment.asFragment(), entry.getTag());
                        frames[i - 1] = new FragmentLayoutController.FragmentEntry(newFragment, entry.getTag());
                    }
                }

                frameCount--;
            }

            fragmentTn.add(myFrameIds[frameCount], entry.getFragment().asFragment(), entry.getTag());
            frames[frameCount++] = entry;

            fragmentTn.addToBackStack(null);
        } finally {
            if (fragmentTn != null) {
                tnId = fragmentTn.commit();
                fragmentTn = null;
            }
        }

        if (tnId >= 0) {
            lastTnId = tnId;
        }
    }

    return lastTnId;
}

From source file:com.android.dialer.DialtactsFragment.java

/**
 * @author bbk wangchunhe/*from   w w w . ja v a  2  s .  c  o m*/
 * @Date 2016/07/15
 * show CallLogFragment of filterType
 * @param filterType
 */
private void showCallLogFragment(int filterType) {

    final FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    if (mCalllogList != null) {
        transaction.remove(mCalllogList);
    }

    mCalllogList = new CallLogFragment(filterType);
    transaction.add(R.id.dialtacts_frame, mCalllogList, "mCalllogList");
    mCalllogList.setRecyclerViewChangedImpl(mRecyclerViewChangedImpl);
    transaction.show(mCalllogList);
    transaction.commitAllowingStateLoss();

}

From source file:org.iota.wallet.ui.activity.MainActivity.java

/**
 * Shows a fragment and hides the old one if there was a fragment previously visible
 *//*from   w  w w. j a  va  2 s. c o  m*/
private void showFragment(Fragment fragment, boolean addToBackStack, boolean killFragments) {

    if (fragment == null) {
        // Do nothing
        return;
    }

    FragmentManager fragmentManager = getFragmentManager();
    Fragment currentFragment = fragmentManager.findFragmentByTag(currentFragmentTag);

    if (currentFragment != null && currentFragment.getClass().getName().equals(fragment.getClass().getName())) {
        // Fragment already shown, do nothing
        return;
    }

    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

    if (killFragments) {
        Class[] fragmentsToKill = { AboutFragment.class, GenerateQRCodeFragment.class, NeighborsFragment.class,
                NodeInfoFragment.class, PasswordLoginFragment.class, QRScannerFragment.class,
                SeedLoginFragment.class, SettingsFragment.class, TangleExplorerTabFragment.class,
                NewTransferFragment.class, WalletAddressesFragment.class, WalletTabFragment.class,
                WalletTransfersFragment.class };
        for (Class fragmentClass : fragmentsToKill) {
            String tag = fragmentClass.getSimpleName();
            if (tag.equals(fragment.getClass().getSimpleName())) {
                continue;
            }
            Fragment fragmentToKill = fragmentManager.findFragmentByTag(tag);
            if (fragmentToKill != null) {
                fragmentTransaction.remove(fragmentToKill);
            }
        }
    }

    fragmentTransaction.setCustomAnimations(R.animator.fade_in, R.animator.fade_out, R.animator.fade_in,
            R.animator.fade_out);

    if (currentFragment != null) {
        // Hide old fragment
        fragmentTransaction.hide(currentFragment);
    }

    String tag = fragment.getClass().getSimpleName();
    Fragment cachedFragment = fragmentManager.findFragmentByTag(tag);
    if (cachedFragment != null) {
        // Cached fragment available
        fragmentTransaction.show(cachedFragment);
    } else {
        fragmentTransaction.add(FRAGMENT_CONTAINER_ID, fragment, tag);
    }
    if (addToBackStack) {
        fragmentTransaction.addToBackStack(null);
    }
    fragmentTransaction.commit();

    if (fragment instanceof OnBackPressedClickListener) {
        onBackPressedClickListener = (OnBackPressedClickListener) fragment;
    } else
        onBackPressedClickListener = null;

    // setChecked if open from WalletItemDialog
    if (fragment instanceof TangleExplorerTabFragment)
        navigationView.getMenu().findItem(R.id.nav_tangle_explorer).setChecked(true);

    currentFragmentTag = tag;
}

From source file:com.android.dialer.DialtactsFragment.java

/**
 *?//from  w  w  w  .j  a va  2  s.c o m
 **/
private void addCallLogFragmentInList() {
    final FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    if (getCallLogFragment() == null) {
        mCalllogList = null;
        mCalllogList = new CallLogFragment(CallLogQueryHandler.CALL_TYPE_ALL);
        transaction.add(R.id.dialtacts_frame, mCalllogList, "mCalllogList");
    } else {
        mCalllogList = (CallLogFragment) getCallLogFragment();
    }
    mCalllogList.setRecyclerViewChangedImpl(mRecyclerViewChangedImpl);
    transaction.show(mCalllogList);
    transaction.commitAllowingStateLoss();
}

From source file:org.opendatakit.tables.activities.TableDisplayActivity.java

public void showListFragment(boolean createNew) {
    this.setCurrentFragmentType(ViewFragmentType.LIST);
    this.updateChildViewVisibility(ViewFragmentType.LIST);
    // Try to use a passed file name. If one doesn't exist, try to use the
    // default.//from ww w .j  a  v a 2s  .  c om
    String fileName = IntentUtil.retrieveFileNameFromBundle(this.getIntent().getExtras());
    if (fileName == null) {
        SQLiteDatabase db = null;
        try {
            db = DatabaseFactory.get().getDatabase(this, getAppName());
            fileName = TableUtil.get().getListViewFilename(db, getTableId());
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }
    FragmentManager fragmentManager = this.getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    this.hideAllOtherViewFragments(ViewFragmentType.LIST, fragmentTransaction);
    ListViewFragment listViewFragment = (ListViewFragment) fragmentManager
            .findFragmentByTag(Constants.FragmentTags.LIST);
    if (listViewFragment == null || createNew) {
        if (listViewFragment == null) {
            WebLogger.getLogger(getAppName()).d(TAG, "[showListFragment] existing list fragment not found");
        } else {
            // remove the old fragment
            WebLogger.getLogger(getAppName()).d(TAG, "[showListFragment] removing old list fragment");
            fragmentTransaction.remove(listViewFragment);
        }
        listViewFragment = this.createListViewFragment(fileName);
        fragmentTransaction.add(R.id.activity_table_display_activity_one_pane_content, listViewFragment,
                Constants.FragmentTags.LIST);
    } else {
        WebLogger.getLogger(getAppName()).d(TAG, "[showListFragment] existing list fragment found");
        fragmentTransaction.show(listViewFragment);
    }
    fragmentTransaction.commit();
}

From source file:com.android.settings.HWSettings.java

private void createFragments() {
    DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);

    this.mViewPager = ((ViewPager) findViewById(R.id.tab_pager));
    this.mPagerAdapter = new SettingsPagerAdapter();
    this.mViewPager.setAdapter(this.mPagerAdapter);
    SettingsPageChangeListener localSettingsPageChangeListener = new SettingsPageChangeListener();
    this.mViewPager.setOnPageChangeListener(localSettingsPageChangeListener);
    this.mFragmentManager = getFragmentManager();
    FragmentTransaction localFragmentTransaction = this.mFragmentManager.beginTransaction();
    Fragment localFragment1 = this.mFragmentManager
            .findFragmentByTag("com.android.settings.HWAllSettingsFragment");
    if (localFragment1 != null)
        localFragmentTransaction.remove(localFragment1);
    Fragment localFragment2 = this.mFragmentManager
            .findFragmentByTag("com.android.settings.HWGeneralSettingsFragment");
    if (localFragment2 != null)
        localFragmentTransaction.remove(localFragment2);
    Log.d("dingjingliang", "createFragments: curTabIndex" + curTabIndex);
    this.mGeneralSettingsFragment = new HWGeneralSettingsFragment(mAuthenticatorHelper, dpm);
    localFragmentTransaction.add(R.id.tab_pager, this.mGeneralSettingsFragment,
            "com.android.settings.HWGeneralSettingsFragment");
    localFragmentTransaction.hide(this.mGeneralSettingsFragment);
    this.mAllSettingsFragment = new HWAllSettingsFragment(mAuthenticatorHelper, dpm);
    localFragmentTransaction.add(R.id.tab_pager, this.mAllSettingsFragment,
            "com.android.settings.HWAllSettingsFragment");
    localFragmentTransaction.hide(this.mAllSettingsFragment);
    localFragmentTransaction.commitAllowingStateLoss();
    this.mFragmentManager.executePendingTransactions();
}

From source file:org.opendatakit.tables.activities.TableDisplayActivity.java

public void showMapFragment(boolean createNew) {
    this.setCurrentFragmentType(ViewFragmentType.MAP);
    this.updateChildViewVisibility(ViewFragmentType.MAP);
    // Set the list view file name.
    String fileName = IntentUtil.retrieveFileNameFromBundle(this.getIntent().getExtras());
    if (fileName == null) {
        SQLiteDatabase db = null;/*from  www.j a va  2s  .  c o m*/
        try {
            db = DatabaseFactory.get().getDatabase(this, getAppName());
            fileName = TableUtil.get().getMapListViewFilename(db, getTableId());
        } finally {
            if (db != null) {
                db.close();
            }
        }
    }
    FragmentManager fragmentManager = this.getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    this.hideAllOtherViewFragments(ViewFragmentType.MAP, fragmentTransaction);
    MapListViewFragment mapListViewFragment = (MapListViewFragment) fragmentManager
            .findFragmentByTag(Constants.FragmentTags.MAP_LIST);
    TableMapInnerFragment innerMapFragment = (TableMapInnerFragment) fragmentManager
            .findFragmentByTag(Constants.FragmentTags.MAP_INNER_MAP);
    if (mapListViewFragment == null || (fileName != null && !fileName.equals(mapListViewFragment.getFileName()))
            || createNew) {
        if (mapListViewFragment != null) {
            // remove the old fragment
            WebLogger.getLogger(getAppName()).d(TAG, "[showMapFragment] removing old map list fragment");
            fragmentTransaction.remove(mapListViewFragment);
        }
        WebLogger.getLogger(getAppName()).d(TAG, "[showMapFragment] creating new map list fragment");
        mapListViewFragment = this.createMapListViewFragment(fileName);
        fragmentTransaction.add(R.id.map_view_list, mapListViewFragment, Constants.FragmentTags.MAP_LIST);
    } else {
        WebLogger.getLogger(getAppName()).d(TAG, "[showMapFragment] existing map list fragment found");
        fragmentTransaction.show(mapListViewFragment);
    }
    if (innerMapFragment == null || createNew) {
        if (innerMapFragment != null) {
            // remove the old fragment
            WebLogger.getLogger(getAppName()).d(TAG, "[showMapFragment] removing old inner map fragment");
            fragmentTransaction.remove(innerMapFragment);
        }
        WebLogger.getLogger(getAppName()).d(TAG, "[showMapFragment] creating new inner map fragment");
        innerMapFragment = this.createInnerMapFragment();
        fragmentTransaction.add(R.id.map_view_inner_map, innerMapFragment,
                Constants.FragmentTags.MAP_INNER_MAP);
        innerMapFragment.listener = this;
    } else {
        WebLogger.getLogger(getAppName()).d(TAG, "[showMapFragment] existing inner map fragment found");
        innerMapFragment.listener = this;
        fragmentTransaction.show(innerMapFragment);
    }
    fragmentTransaction.commit();
}

From source file:org.opendatakit.tables.activities.TableDisplayActivity.java

/**
 * Show the spreadsheet fragment.// ww w .j  a  va  2  s . c  o  m
 * 
 * @param createNew
 */
public void showSpreadsheetFragment(boolean createNew) {
    this.setCurrentFragmentType(ViewFragmentType.SPREADSHEET);
    this.updateChildViewVisibility(ViewFragmentType.SPREADSHEET);
    FragmentManager fragmentManager = this.getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    // Hide all the other fragments.
    this.hideAllOtherViewFragments(ViewFragmentType.SPREADSHEET, fragmentTransaction);
    // Try to retrieve one already there.
    SpreadsheetFragment spreadsheetFragment = (SpreadsheetFragment) fragmentManager
            .findFragmentByTag(Constants.FragmentTags.SPREADSHEET);
    if (spreadsheetFragment == null) {
        WebLogger.getLogger(getAppName()).d(TAG,
                "[showSpreadsheetFragment] no existing spreadshseet " + "fragment found");
    } else {
        WebLogger.getLogger(getAppName()).d(TAG,
                "[showSpreadsheetFragment] existing spreadsheet fragment " + "found");
    }
    WebLogger.getLogger(getAppName()).d(TAG, "[showSpreadsheetFragment] createNew is: " + createNew);
    if (spreadsheetFragment == null || createNew) {
        if (spreadsheetFragment != null) {
            WebLogger.getLogger(getAppName()).d(TAG, "[showSpreadsheetFragment] removing existing fragment");
            // Get rid of the existing fragment
            fragmentTransaction.remove(spreadsheetFragment);
        }
        spreadsheetFragment = this.createSpreadsheetFragment();
        fragmentTransaction.add(R.id.activity_table_display_activity_one_pane_content, spreadsheetFragment,
                Constants.FragmentTags.SPREADSHEET);
    } else {
        fragmentTransaction.show(spreadsheetFragment);
    }
    fragmentTransaction.commit();
}