Example usage for android.app FragmentTransaction replace

List of usage examples for android.app FragmentTransaction replace

Introduction

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

Prototype

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

Source Link

Document

Replace an existing fragment that was added to a container.

Usage

From source file:com.example.android.AudioArchive.ui.MusicPlayerActivity.java

private void navigateToBrowser(String mediaId) {
    LogHelper.d(TAG, "navigateToBrowser, mediaId=" + mediaId);
    MediaBrowserFragment fragment = getBrowseFragment();

    if (fragment == null || !TextUtils.equals(fragment.getMediaId(), mediaId)) {
        fragment = new MediaBrowserFragment();
        fragment.setMediaId(mediaId);/*from w w w. j a v  a2 s  .c o m*/
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.animator.slide_in_from_right, R.animator.slide_out_to_left,
                R.animator.slide_in_from_left, R.animator.slide_out_to_right);
        transaction.replace(R.id.container, fragment, FRAGMENT_TAG);
        // If this is not the top level media (root), we add it to the fragment back stack,
        // so that actionbar toggle and Back will work appropriately:
        if (mediaId != null) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }
}

From source file:org.steveleach.scoresheet.ui.ScoresheetActivity.java

public void showFragment(Fragment fragment) {
    FragmentTransaction tx = getFragmentManager().beginTransaction();
    tx.replace(R.id.fragmentContainer, fragment, MAIN_FRAGMENT);
    tx.addToBackStack(null);//from  w  w  w. j av  a2 s.  com
    tx.commit();
}

From source file:com.learnit.LearnIt.activities.MainActivity.java

public void onArticleSelected(int position) {
    // Create a new listOfFragments
    MySmartFragment fragment;/* ww  w  . ja va2s.c o m*/
    switch (position) {
    case ADD_WORDS_FRAGMENT:
        fragment = new AddWordFragment();
        fragment.identifier = ADD_WORDS_FRAGMENT;
        Log.d(LOG_TAG, "Created AddWordFragment with tag " + fragment.identifier);
        break;
    case DICTIONARY_FRAGMENT:
        fragment = new DictFragment();
        fragment.identifier = DICTIONARY_FRAGMENT;
        Log.d(LOG_TAG, "Created Dictionary Fragment with tag " + fragment.identifier);
        break;
    case LEARN_WORDS_FRAGMENT:
        fragment = new LearnCasualFragment();
        fragment.identifier = LEARN_WORDS_FRAGMENT;
        Log.d(LOG_TAG, "Created LearnFragment with tag " + fragment.identifier);
        break;
    default:
        fragment = null;
    }
    // Update the layout
    FragmentManager fm = getFragmentManager();

    if (fm.findFragmentByTag("android:switcher:" + 0 + ":" + position) == null) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.setCustomAnimations(android.R.animator.fade_in, android.R.animator.fade_out);
        ft.replace(R.id.view_group_id, fragment, "android:switcher:" + 0 + ":" + position);
        Log.d(LOG_TAG, "current fragment id = " + fragment.getId() + " and tag = " + fragment.getTag()
                + ((Object) fragment).getClass().getName());
        ft.commit();
    }
    _currentItemShown = position;
    Log.d(LOG_TAG, "onArticleSelected current item set to " + _currentItemShown);
}

From source file:android.support.v17.leanback.app.GuidedStepFragment.java

/**
 * Adds the specified GuidedStepFragment as content of Activity; no backstack entry is added so
 * the activity will be dismissed when BACK key is pressed.  The method is typically called in
 * Activity.onCreate() when savedInstanceState is null.  When savedInstanceState is not null,
 * the Activity is being restored,  do not call addAsRoot() to duplicate the Fragment restored
 * by FragmentManager.//from   w w w . j  a  v  a2  s.co  m
 * {@link #UI_STYLE_ACTIVITY_ROOT} is assigned.
 *
 * Note: currently fragments added using this method must be created programmatically rather
 * than via XML.
 * @param activity The Activity to be used to insert GuidedstepFragment.
 * @param fragment The GuidedStepFragment to be inserted into the fragment stack.
 * @param id The id of container to add GuidedStepFragment, can be android.R.id.content.
 * @return The ID returned by the call FragmentTransaction.commit, or -1 there is already
 *         GuidedStepFragment.
 */
public static int addAsRoot(Activity activity, GuidedStepFragment fragment, int id) {
    // Workaround b/23764120: call getDecorView() to force requestFeature of ActivityTransition.
    activity.getWindow().getDecorView();
    FragmentManager fragmentManager = activity.getFragmentManager();
    if (fragmentManager.findFragmentByTag(TAG_LEAN_BACK_ACTIONS_FRAGMENT) != null) {
        Log.w(TAG, "Fragment is already exists, likely calling "
                + "addAsRoot() when savedInstanceState is not null in Activity.onCreate().");
        return -1;
    }
    FragmentTransaction ft = fragmentManager.beginTransaction();
    fragment.setUiStyle(UI_STYLE_ACTIVITY_ROOT);
    return ft.replace(id, fragment, TAG_LEAN_BACK_ACTIONS_FRAGMENT).commit();
}

From source file:com.github.cpmproto.categorystepfragment.base.GuidedStepListFragment.java

/**
 * Adds the specified GuidedStepListFragment to the fragment stack, replacing any existing
 * GuidedStepListFragments in the stack, and configuring the fragment-to-fragment custom
 * transitions.  A backstack entry is added, so the fragment will be dismissed when BACK key
 * is pressed./*  w ww .ja v a2s.  c  o m*/
 * <li>If current fragment on stack is GuidedStepListFragment: assign {@link #UI_STYLE_REPLACE} and
 * {@link #onAddSharedElementTransition(FragmentTransaction, GuidedStepListFragment)} will be called
 * to perform shared element transition between GuidedStepListFragments.
 * <li>If current fragment on stack is not GuidedStepListFragment: assign {@link #UI_STYLE_ENTRANCE}
 * <p/>
 * Note: currently fragments added using this method must be created programmatically rather
 * than via XML.
 *
 * @param fragmentManager The FragmentManager to be used in the transaction.
 * @param fragment        The GuidedStepListFragment to be inserted into the fragment stack.
 * @param id              The id of container to add GuidedStepListFragment, can be android.R.id.content.
 * @return The ID returned by the call FragmentTransaction.commit.
 */
public static int add(FragmentManager fragmentManager, GuidedStepListFragment fragment, int id) {
    GuidedStepListFragment current = getCurrentGuidedStepListFragment(fragmentManager);
    boolean inGuidedStep = current != null;
    if (IS_FRAMEWORK_FRAGMENT && Build.VERSION.SDK_INT >= 21 && Build.VERSION.SDK_INT < 23 && !inGuidedStep) {
        // workaround b/22631964 for framework fragment
        fragmentManager.beginTransaction().replace(id, new DummyFragment(), TAG_LEAN_BACK_ACTIONS_FRAGMENT)
                .commit();
    }
    FragmentTransaction ft = fragmentManager.beginTransaction();

    fragment.setUiStyle(inGuidedStep ? UI_STYLE_REPLACE : UI_STYLE_ENTRANCE);
    ft.addToBackStack(fragment.generateStackEntryName());
    if (current != null) {
        fragment.onAddSharedElementTransition(ft, current);
    }
    return ft.replace(id, fragment, TAG_LEAN_BACK_ACTIONS_FRAGMENT).commit();
}

From source file:rocks.stalin.android.app.ui.MusicPlayerActivity.java

private void navigateToBrowser(String mediaId) {
    Log.d(TAG, "navigateToBrowser, mediaId=" + mediaId);
    MediaBrowserFragment fragment = getBrowseFragment();

    if (fragment == null || !TextUtils.equals(fragment.getMediaId(), mediaId)) {
        fragment = new MediaBrowserFragment();
        fragment.setMediaId(mediaId);// www  . j av a2s . c  om
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.animator.slide_in_from_right, R.animator.slide_out_to_left,
                R.animator.slide_in_from_left, R.animator.slide_out_to_right);
        transaction.replace(R.id.container, fragment, FRAGMENT_TAG);
        // If this is not the top level media (root), we add it to the fragment back stack,
        // so that actionbar toggle and Back will work appropriately:
        if (mediaId != null) {
            transaction.addToBackStack(null);
        }
        transaction.commit();
    }
}

From source file:uk.org.downiesoft.slideshow.SlideShowActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Window win = getWindow();/*from  w  ww . j  a  v a  2 s .c om*/
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
    win.setFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION,
            WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
    win.requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    mUiHider = new UiHider(this);
    getWindow().getDecorView().setOnSystemUiVisibilityChangeListener(mUiHider);
    mCacheManager = ThumbnailCacheManager.getInstance(this);
    SlideShowActivity.debug(1, TAG, "onCreate: %s",
            savedInstanceState == null ? "null" : savedInstanceState.toString());
    setContentView(R.layout.activity_slide_show);
    PreferenceManager.setDefaultValues(this, R.xml.view_preferences, false);
    PreferenceManager.setDefaultValues(this, R.xml.slideshow_preferences, false);
    PreferenceManager.setDefaultValues(this, R.xml.cache_preferences, false);
    PreferenceManager.setDefaultValues(this, R.xml.wallpaper_preferences, false);
    PreferenceManager.setDefaultValues(this, R.xml.other_preferences, false);
    mSlideshowSettings = PreferenceManager.getDefaultSharedPreferences(this);
    int thumbSize = Integer.parseInt(mSlideshowSettings.getString(getString(R.string.PREFS_THUMBSIZE), "1"));
    // Hack to convert old hard-coded thumbsize settings to platform dependent sizes
    if (thumbSize > 2) {
        thumbSize = thumbSize / 90;
        SharedPreferences.Editor editor = mSlideshowSettings.edit();
        editor.putString(getString(R.string.PREFS_THUMBSIZE), Integer.toString(thumbSize));
        editor.apply();
    }
    FragmentManager fm = getFragmentManager();
    int cacheLimit = Integer
            .parseInt(mSlideshowSettings.getString(getString(R.string.PREFS_CACHE_LIMIT), "10"));
    mPreviewButton = (ImageView) findViewById(R.id.slideShowImageButton);
    mPreviewView = (FrameLayout) findViewById(R.id.previewContainer);
    RelativeLayout divider = (RelativeLayout) findViewById(R.id.frameDivider);
    if (divider != null) {
        divider.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {
                return mGestureDetector.onTouchEvent(event) || view.onTouchEvent(event);
            }
        });
    }
    mPreviewFragment = (PreviewFragment) fm.findFragmentByTag(PreviewFragment.TAG);
    if (mPreviewView != null && mPreviewFragment == null) {
        mPreviewFragment = new PreviewFragment();
        fm.beginTransaction().replace(R.id.previewContainer, mPreviewFragment, PreviewFragment.TAG).commit();
    }
    mGridViewFragment = (GridViewFragment) fm.findFragmentByTag(GridViewFragment.TAG);
    if (mGridViewFragment == null) {
        mGridViewFragment = new GridViewFragment();
        FragmentTransaction ft = fm.beginTransaction();
        ft.replace(R.id.fragmentContainer, mGridViewFragment, GridViewFragment.TAG);
        ft.commit();
    }
    mGestureDetector = new GestureDetector(this, new DividerGestureListener());
    // restart/stop service as required
    Intent intent = getIntent();
    if (intent != null && intent.getAction() != null && intent.getAction().equals(ACTION_STOP_WALLPAPER)
            && isServiceRunning()) {
        stopWallpaperService();
        finish();
    } else {
        if (!mSlideshowSettings.getBoolean(getString(R.string.PREFS_WALLPAPER_ENABLE), false)) {
            if (isServiceRunning()) {
                stopWallpaperService();
            }
        } else {
            if (!isServiceRunning()) {
                Intent startIntent = new Intent(SlideShowActivity.this, WallpaperService.class);
                startService(startIntent);
                invalidateOptionsMenu();
            }
        }
    }
    mCacheManager.tidyCache(cacheLimit);
    BitmapManager.setDisplayMetrics(getResources().getDisplayMetrics());
}

From source file:com.bayapps.android.robophish.ui.MusicPlayerActivity.java

private void navigateToBrowser(String title, String subtitle, String mediaId) {
    LogHelper.d(TAG, "navigateToBrowser, mediaId=" + mediaId);
    MediaBrowserFragment fragment = getBrowseFragment();

    if (fragment == null || !TextUtils.equals(fragment.getMediaId(), mediaId)) {
        fragment = new MediaBrowserFragment();
        fragment.setMediaId(title, subtitle, mediaId);
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.setCustomAnimations(R.animator.slide_in_from_right, R.animator.slide_out_to_left,
                R.animator.slide_in_from_left, R.animator.slide_out_to_right);
        transaction.replace(R.id.container, fragment, FRAGMENT_TAG);
        // If this is not the top level media (root), we add it to the fragment back stack,
        // so that actionbar toggle and Back will work appropriately:
        if (mediaId != null) {
            transaction.addToBackStack(null);
        }/*from  ww  w  . j  a  va 2  s  .co m*/
        transaction.commit();
    }
}

From source file:com.concentricsky.android.khanacademy.app.HomeActivity.java

private void setListForTopic(final Topic topic, final Class<? extends AbstractListFragment<?>> fragmentClass,
        final boolean forward) {

    this.requestDataService(new ObjectCallback<KADataService>() {
        @Override// www .j a  v  a2s  .com
        public void call(KADataService dataService) {
            try {
                final Fragment frag = fragmentClass.newInstance();
                Bundle args = new Bundle();
                args.putString(PARAM_TOPIC_ID, topic.getId());
                frag.setArguments(args);

                // transition the fragments
                FragmentTransaction tx = getFragmentManager().beginTransaction()
                        .setBreadCrumbTitle(topic.getTitle());

                tx.replace(R.id.activity_home_list_container, frag, TAG_LIST_FRAGMENT).commit();

            } catch (InstantiationException e) {
                // Swallow this; we know that both AbstractListFragment subclasses have zero-arg constructors.
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // Swallow this; we know that both AbstractListFragment subclasses are visible here.
                e.printStackTrace();
            }
        }
    });

}

From source file:leoisasmendi.android.com.suricatepodcast.MainActivity.java

private void showSearchFragment() {
    SearchFragment searchFragment = new SearchFragment();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    if (getResources().getBoolean(R.bool.twoPaneMode)) {
        fragmentTransaction.replace(R.id.detail_container, searchFragment,
                SearchFragment.class.getSimpleName());
    } else {//  w ww.j  a  v  a  2s .co m
        fragmentTransaction.replace(R.id.master_container, searchFragment);
    }

    fragmentTransaction.addToBackStack(SearchFragment.class.getSimpleName()).commit();
}