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:org.chromium.chrome.browser.bookmark.ManageBookmarkActivity.java

/**
 * Creates and sets an action listener on the add/edit bookmark fragment.
 * @param addEditFragment The fragment that the listener should be attached to.
 *///from  w  w  w . j a  v a2 s  .co  m
private void setActionListenerOnAddEdit(final AddEditBookmarkFragment addEditFragment) {
    addEditFragment.setOnActionListener(new AddEditBookmarkFragment.OnActionListener() {
        @Override
        public void onCancel() {
            finishAddEdit();
        }

        @Override
        public void onNodeEdited(long id) {
            finishAddEdit();
        }

        @Override
        public void onFolderCreated(long id, String name) {
            finishAddEdit();
            if (getFragmentManager().getBackStackEntryCount() != 0) {
                ((SelectBookmarkFolderFragment) addEditFragment.getTargetFragment()).executeFolderSelection(id,
                        name);
            }
        }

        @Override
        public void triggerFolderSelection() {
            FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

            boolean isRoot = getFragmentManager().getBackStackEntryCount() == 0;
            SelectBookmarkFolderFragment selectFolder = SelectBookmarkFolderFragment.newInstance(
                    // Only allow the root add/edit bookmark UI to create new bookmark
                    // folders during selection.
                    isRoot, addEditFragment.getParentFolderId(), addEditFragment.isFolder());
            selectFolder.setTargetFragment(addEditFragment, 0);
            setActionListenerOnFolderSelection(selectFolder);

            String tag = isRoot ? BASE_SELECT_FOLDER_FRAGMENT_TAG : ADD_FOLDER_SELECT_FOLDER_FRAGMENT_TAG;
            fragmentTransaction.add(android.R.id.content, selectFolder, tag);
            fragmentTransaction.hide(addEditFragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }

        @Override
        public void onRemove() {
            finishAddEdit();
        }

        @Override
        public void setBackEnabled(boolean enabled) {
            mIsBackEnabled = enabled;
        }

        private void finishAddEdit() {
            if (getFragmentManager().getBackStackEntryCount() == 0) {
                finish();
            } else {
                getFragmentManager().popBackStackImmediate();
            }
        }
    });
}

From source file:am.project.x.business.main.MainActivity.java

private void setFragment(String tag) {
    final String oldTag = mCurrent;
    mCurrent = tag;//from   w w w.  ja va 2  s.  c om
    final FragmentManager manager = getFragmentManager();
    final FragmentTransaction transaction = manager.beginTransaction();
    if (!TextUtils.isEmpty(oldTag)) {
        final Fragment old = manager.findFragmentByTag(oldTag);
        if (old != null && old.isVisible()) {
            transaction.hide(old);
        }
    }
    Fragment target = manager.findFragmentByTag(tag);
    if (target != null && target.isVisible())
        return;
    if (target == null) {
        switch (tag) {
        default:
        case TAG_WIDGETS:
            target = WidgetsFragment.newInstance();
            break;
        case TAG_DRAWABLES:
            target = DrawablesFragment.newInstance();
            break;
        case TAG_OTHERS:
            target = OthersFragment.newInstance();
            break;
        case TAG_DEVELOP:
            target = DevelopFragment.newInstance();
            break;
        }
        transaction.add(mVContent.getId(), target, tag);
    }
    transaction.show(target);
    transaction.commit();
}

From source file:android.support.v13.app.FragmentTabHost.java

private FragmentTransaction doTabChanged(String tabId, FragmentTransaction ft) {
    TabInfo newTab = null;/*  w w w  .  j av  a 2  s.c  om*/
    for (int i = 0; i < mTabs.size(); i++) {
        TabInfo tab = mTabs.get(i);
        if (tab.tag.equals(tabId)) {
            newTab = tab;
        }
    }
    if (newTab == null) {
        throw new IllegalStateException("No tab known for tag " + tabId);
    }
    if (mLastTab != newTab) {
        if (ft == null) {
            ft = mFragmentManager.beginTransaction();
        }
        if (mLastTab != null) {
            if (mLastTab.fragment != null) {
                ft.detach(mLastTab.fragment);
            }
        }
        if (newTab != null) {
            if (newTab.fragment == null) {
                newTab.fragment = Fragment.instantiate(mContext, newTab.clss.getName(), newTab.args);
                ft.add(mContainerId, newTab.fragment, newTab.tag);
            } else {
                ft.attach(newTab.fragment);
            }
        }

        mLastTab = newTab;
    }
    return ft;
}

From source file:com.geecko.QuickLyric.MainActivity.java

private LyricsViewFragment init(FragmentManager fragmentManager, boolean startEmpty) {
    LyricsViewFragment lyricsViewFragment = (LyricsViewFragment) fragmentManager
            .findFragmentByTag(LYRICS_FRAGMENT_TAG);
    if (lyricsViewFragment == null || lyricsViewFragment.isDetached())
        lyricsViewFragment = new LyricsViewFragment();
    lyricsViewFragment.startEmpty(startEmpty);
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.setCustomAnimations(R.animator.slide_in_end, R.animator.slide_out_start,
            R.animator.slide_in_start, R.animator.slide_out_end);
    if (!lyricsViewFragment.isAdded()) {
        fragmentTransaction.add(id.main_fragment_container, lyricsViewFragment, LYRICS_FRAGMENT_TAG);
    }/*from ww w  .ja v a2s  .  com*/

    Fragment[] activeFragments = getActiveFragments();
    displayedFragment = getDisplayedFragment(activeFragments);

    for (Fragment fragment : activeFragments)
        if (fragment != null) {
            if (fragment != displayedFragment && !fragment.isHidden()) {
                fragmentTransaction.hide(fragment);
                fragment.onHiddenChanged(true);
            } else if (fragment == displayedFragment)
                fragmentTransaction.show(fragment);
        }
    fragmentTransaction.commit();
    return lyricsViewFragment;
}

From source file:cz.metaverse.android.bilingualreader.ReaderActivity.java

/**
 * Adds a panel to view for the first time.
 * @param p SplitPanel instance/*from w  w  w. ja va2s .c  o m*/
 */
public void addPanel(SplitPanel p) {
    Log.d(LOG, "ReaderActivity.addPanel");

    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.add(R.id.PanelsLayout, p, p.getTag());
    fragmentTransaction.commit();

    panelCount++;

    // Rethinks what menu items to display
    invalidateOptionsMenu();
}

From source file:com.google.android.apps.forscience.whistlepunk.review.RunReviewFragment.java

@Override
public void onAddNoteTimestampClicked(GoosciLabelValue.LabelValue selectedValue, int labelType,
        long selectedTimestamp) {
    AddNoteDialog addDialog = (AddNoteDialog) getChildFragmentManager().findFragmentByTag(AddNoteDialog.TAG);
    if (addDialog != null) {
        addDialog.dismiss();//  w w  w  .  java 2 s. com
    }

    // Show the timestamp edit window below the graph / over the notes
    getView().findViewById(R.id.embedded).setVisibility(View.VISIBLE);
    EditTimeDialog timeDialog = EditTimeDialog.newInstance(selectedValue, labelType, selectedTimestamp,
            mExperimentRun.getFirstTimestamp());
    FragmentTransaction ft = getChildFragmentManager().beginTransaction();
    ft.add(R.id.embedded, timeDialog, EditTimeDialog.TAG);
    ft.commit();
    mRunReviewOverlay.setActiveTimestamp(selectedTimestamp);
    mRunReviewOverlay.setOnTimestampChangeListener(timeDialog);
    setTimepickerUi(getView(), true);
}

From source file:com.google.android.apps.forscience.whistlepunk.review.RunReviewFragment.java

@Override
public void onEditNoteTimestampClicked(Label label, GoosciLabelValue.LabelValue selectedValue,
        long labelTimestamp) {
    // Dismiss the edit note dialog and show the timestamp dialog.
    EditNoteDialog editDialog = (EditNoteDialog) getChildFragmentManager()
            .findFragmentByTag(EditNoteDialog.TAG);
    if (editDialog != null) {
        editDialog.dismiss();//  w ww . ja v a2 s . c  o  m
    }

    // Show the timestamp edit window below the graph / over the notes
    getView().findViewById(R.id.embedded).setVisibility(View.VISIBLE);
    EditTimeDialog timeDialog = EditTimeDialog.newInstance(label, selectedValue, labelTimestamp,
            mExperimentRun.getFirstTimestamp());
    FragmentTransaction ft = getChildFragmentManager().beginTransaction();
    ft.add(R.id.embedded, timeDialog, EditTimeDialog.TAG);
    ft.commit();
    mRunReviewOverlay.setActiveTimestamp(labelTimestamp);
    mRunReviewOverlay.setOnTimestampChangeListener(timeDialog);
    setTimepickerUi(getView(), true);
}

From source file:net.sf.sprockets.app.ui.PanesActivity.java

/**
 * Use your own layout for the panes./*from  www  .j a  v a 2 s . c  o m*/
 *
 * @param pagerId {@code R.id} value for the ViewPager in the single pane layout
 * @param pane1Id {@code R.id} value for the first pane in the multi-pane layout
 * @param pane2Id {@code R.id} value for the second pane in the multi-pane layout
 */
public void setContentView(int layoutResId, int pagerId, int pane1Id, int pane2Id) {
    setContentView(layoutResId);
    Fragment pane1 = findFragmentByPane(1);
    Fragment pane2 = findFragmentByPane(2);
    ViewPager pager = findById(this, pagerId);
    /* do we need to move the fragments between the single and multi-pane layouts? */
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = null;
    if (pane2 == null) {
        pane2 = getFragment(2);
    } else if (pane2.getId() != (pager != null ? pagerId : pane2Id)) {
        ft = fm.beginTransaction().remove(pane2); // remove in reverse to preserve indices
    }
    if (pane1 == null) {
        pane1 = getFragment(1);
    } else if (pane1.getId() != (pager != null ? pagerId : pane1Id)) {
        if (ft == null) {
            ft = fm.beginTransaction();
        }
        ft.remove(pane1);
    }
    if (ft != null) {
        ft.commitAllowingStateLoss();
        fm.executePendingTransactions(); // force removes so can add to a different container
    }
    /* add the fragments to the panes */
    if (pager != null) {
        pager.setAdapter(new PanesAdapter(pane1, pane2));
    } else {
        ft = null;
        if (pane1.getId() != pane1Id) {
            ft = Fragments.open(this).add(pane1Id, pane1, PANE_1);
        }
        if (pane2.getId() != pane2Id) {
            if (ft == null) {
                ft = Fragments.open(this);
            }
            ft.add(pane2Id, pane2, PANE_2);
        }
        if (ft != null) {
            ft.commitAllowingStateLoss();
        }
    }
}

From source file:com.geecko.QuickLyric.MainActivity.java

public void updateLyricsFragment(int outAnim, String... params) { // Should only be called from SearchFragment or IdDecoder
    String artist = params[0];/*from www .  j  av  a  2 s  . c  om*/
    String song = params[1];
    String url = null;
    if (params.length > 2)
        url = params[2];
    LyricsViewFragment lyricsViewFragment = (LyricsViewFragment) getFragmentManager()
            .findFragmentByTag(LYRICS_FRAGMENT_TAG);
    if (lyricsViewFragment != null)
        lyricsViewFragment.fetchLyrics(artist, song, url);
    else {
        Lyrics lyrics = new Lyrics(Lyrics.SEARCH_ITEM);
        lyrics.setArtist(artist);
        lyrics.setTitle(song);
        lyrics.setURL(url);
        Bundle lyricsBundle = new Bundle();
        try {
            if (artist != null && song != null)
                lyricsBundle.putByteArray("lyrics", lyrics.toBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
        lyricsViewFragment = new LyricsViewFragment();
        lyricsViewFragment.setArguments(lyricsBundle);

        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.animator.slide_in_start, outAnim, R.animator.slide_in_start,
                outAnim);
        Fragment activeFragment = getDisplayedFragment(getActiveFragments());
        if (activeFragment != null) {
            prepareAnimations(activeFragment);
            fragmentTransaction.hide(activeFragment);
        }
        fragmentTransaction.add(id.main_fragment_container, lyricsViewFragment, LYRICS_FRAGMENT_TAG);
        lyricsViewFragment.isActiveFragment = true;
        fragmentTransaction.commit();
    }
    if (drawer instanceof DrawerLayout && !mDrawerToggle.isDrawerIndicatorEnabled()) {
        mDrawerToggle.setDrawerIndicatorEnabled(true);
        ((DrawerLayout) drawer).setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
    }
}

From source file:com.geecko.QuickLyric.MainActivity.java

private void selectItem(int position) {
    FragmentManager fragmentManager = getFragmentManager();
    Fragment newFragment;// w w w. j  a va2s .c  o  m
    String tag;
    switch (position) {
    default:
        // Lyrics
        tag = LYRICS_FRAGMENT_TAG;
        newFragment = fragmentManager.findFragmentByTag(tag);
        if (newFragment == null || !(newFragment instanceof LyricsViewFragment))
            newFragment = new LyricsViewFragment();
        ((LyricsViewFragment) newFragment).showTransitionAnim = true;
        break;
    case 1:
        // Saved Lyrics
        tag = LOCAL_LYRICS_FRAGMENT_TAG;
        newFragment = fragmentManager.findFragmentByTag(tag);
        if (newFragment == null || !(newFragment instanceof LocalLyricsFragment))
            newFragment = new LocalLyricsFragment();
        ((LocalLyricsFragment) newFragment).showTransitionAnim = true;
        break;
    case 2:
        // Separator
        return;
    case 3:
        // Settings
        if (drawer instanceof DrawerLayout)
            ((DrawerLayout) drawer).closeDrawer(drawerView);
        drawer.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent settingsIntent = new Intent(MainActivity.this, SettingsActivity.class);
                startActivityForResult(settingsIntent, 77);
            }
        }, 250);
        return;
    case 4:
        // Feedback
        return;
    case 5:
        // About Dialog
        if (drawer instanceof DrawerLayout)
            ((DrawerLayout) drawer).closeDrawer(drawerView);
        drawer.postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent aboutIntent = new Intent(MainActivity.this, AboutActivity.class);
                startActivityForResult(aboutIntent, 77);
            }
        }, 250);
        return;
    }

    final Fragment activeFragment = getDisplayedFragment(getActiveFragments());
    prepareAnimations(activeFragment);

    // Insert the fragment by replacing any existing fragment
    if (newFragment != activeFragment) {
        if (mActionMode != null)
            mActionMode.finish();
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.setCustomAnimations(R.animator.slide_in_start, R.animator.slide_out_start,
                R.animator.slide_in_start, R.animator.slide_out_start);
        fragmentTransaction.hide(activeFragment);
        if (newFragment.isAdded())
            fragmentTransaction.show(newFragment);
        else
            fragmentTransaction.add(id.main_fragment_container, newFragment, tag);
        ((CollapsingToolbarLayout) findViewById(R.id.toolbar_layout)).setCollapsedTitleTextColor(Color.WHITE);
        fragmentTransaction.commit();

        if (activeFragment instanceof LyricsViewFragment || newFragment instanceof LyricsViewFragment) {
            final Fragment newFragmentCopy = newFragment;
            activeFragment.getView().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (activeFragment instanceof LyricsViewFragment) {
                        expandToolbar(false);
                        showRefreshFab(false);
                    } else if (newFragmentCopy instanceof LyricsViewFragment) {
                        expandToolbar(true);
                        showRefreshFab(true);
                    }
                }
            }, getResources().getInteger(android.R.integer.config_longAnimTime));
        }
    }
    if (drawer instanceof DrawerLayout && (newFragment == activeFragment))
        ((DrawerLayout) drawer).closeDrawer(drawerView);
}