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:android.support.v17.leanback.app.GuidedStepFragment.java

/**
 * Adds the specified GuidedStepFragment to the fragment stack, replacing any existing
 * GuidedStepFragments 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./*from   www  .jav a  2s .  c o m*/
 * <li>If current fragment on stack is GuidedStepFragment: assign {@link #UI_STYLE_REPLACE} and
 * {@link #onAddSharedElementTransition(FragmentTransaction, GuidedStepFragment)} will be called
 * to perform shared element transition between GuidedStepFragments.
 * <li>If current fragment on stack is not GuidedStepFragment: 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 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.
 */
public static int add(FragmentManager fragmentManager, GuidedStepFragment fragment, int id) {
    GuidedStepFragment current = getCurrentGuidedStepFragment(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:com.aokp.romcontrol.github.tasks.DisplayProjectsListTask.java

private void loadProjectsToArray(JSONArray repoProjectsArray) {
    // scroll through each item in array (projects in repo organization)
    for (int i = 0; i < repoProjectsArray.length(); i++) {
        try {//from  w  w w .  j a  v a  2  s  .com
            final JSONObject projectsObject = (JSONObject) repoProjectsArray.get(i);
            final Preference mProject = mCategory.getPreferenceManager().createPreferenceScreen(mContext);
            // extract info about each project
            final String projectName = projectsObject.getString("name");
            String projectDescription = projectsObject.getString("description");
            int githubProjectId = projectsObject.getInt("id");
            // apply info to our preference screen
            mProject.setKey(projectName);
            if (projectDescription.contains("") || projectDescription == null) {
                mProject.setTitle(projectName);
                mProject.setSummary(projectDescription);
            } else {
                mProject.setTitle(projectDescription);
                mProject.setSummary(projectName);
            }
            mProject.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
                @Override
                public boolean onPreferenceClick(Preference p) {
                    AlertDialog.Builder adb = new AlertDialog.Builder(mAlertDialog.getContext());
                    if (!mFavPackagesStorage.isFavProject(projectName)) {
                        adb.setNegativeButton(R.string.changelog_add_to_favs_list,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Log.d(TAG, "fav packages size=="
                                                + mFavPackagesStorage.getFavProjects().size());
                                        if (mFavPackagesStorage.getFavProjects().size() > 0) {
                                            mPreferenceScreen.addPreference(mFavProjects);
                                        }
                                        mFavPackagesStorage.addProject(projectName);
                                        mCategory.removePreference(mProject);
                                        mFavProjects.addPreference(mProject);
                                    }
                                }).setMessage(R.string.add_favs_or_view);
                    } else {
                        adb.setNegativeButton(R.string.changelog_remove_from_favs_list,
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        mFavPackagesStorage.removeProject(projectName);
                                        mFavProjects.removePreference(mProject);
                                        mCategory.addPreference(mProject);
                                        Log.d(TAG, "fav packages size=="
                                                + mFavPackagesStorage.getFavProjects().size());
                                        if (mFavPackagesStorage.getFavProjects().size() == 1) {
                                            mPreferenceScreen.removePreference(mFavProjects);
                                        }
                                    }
                                }).setMessage(R.string.remove_favs_or_view);
                    }
                    adb.setPositiveButton(R.string.changelog_view_commits,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    FragmentTransaction transaction = mFragmentManager.beginTransaction();
                                    CommitsFragment commitFragment = new CommitsFragment(mAlertDialog,
                                            projectName);
                                    transaction.addToBackStack(null);
                                    transaction.replace(mId, commitFragment, projectName);
                                    transaction.commit();
                                }
                            }).create().show();
                    return true;
                }
            });
            if (mFavPackagesStorage.isFavProject(projectName)) {
                if (mFavProjects.findPreference(projectName) == null) {
                    Log.d(TAG, "found Favorite Project: " + projectName);
                    mFavProjects.addPreference(mProject);
                }
            } else {
                if (DEBUG)
                    Log.d(TAG, "adding normal project: " + projectName);
                mCategory.addPreference(mProject);
            }
        } catch (JSONException badJsonRequest) {
            Log.e(TAG, "failed to parse required info about project", badJsonRequest);
        }
    }
    if (mFavPackagesStorage.getFavProjects().size() > 0)
        mPreferenceScreen.addPreference(mFavProjects);
}

From source file:com.android.mail.ui.TwoPaneController.java

@Override
public void launchFragment(final Fragment fragment, final int selectPosition) {
    final int containerViewId = TwoPaneLayout.MISCELLANEOUS_VIEW_ID;

    final FragmentManager fragmentManager = mActivity.getFragmentManager();
    if (fragmentManager.findFragmentByTag(TAG_CUSTOM_FRAGMENT) == null) {
        final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(containerViewId, fragment, TAG_CUSTOM_FRAGMENT);
        mMiscellaneousViewTransactionId = fragmentTransaction.commitAllowingStateLoss();
        fragmentManager.executePendingTransactions();
    }/*  w w w .  ja v  a 2  s  .com*/

    if (selectPosition >= 0) {
        getConversationListFragment().setRawSelected(selectPosition, true);
    }
}

From source file:by.zatta.pilight.MainActivity.java

@Override
public void onLanguageListener(String language) {
    makeLocale(language);/* w w w.  ja  v  a 2  s  . co m*/
    invalidateOptionsMenu();
    initMenu();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    fm.popBackStack();
    ft.replace(R.id.fragment_main, new PrefFragment(), "prefs");
    ft.addToBackStack(null);
    ft.commit();
}

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

/**
 * Ricostruisce la successione dei frammenti nell&rsquo;unico pannello.
 * //from  w  w  w.  j av  a2 s. co m
 * @param  fragmentMgr   Gestore dei frammenti.
 * @param  fragmentQueue Frammenti.
 * @return               Identificatore dell&rsquo;ultimo elemento inserito
 *                       nel back stack. 
 */
private int arrangePanel(FragmentManager fragmentMgr,
        Queue<FragmentLayoutController.FragmentEntry> fragmentQueue) {
    int tnId, lastTnId;
    FragmentLayoutController.FragmentEntry entry;
    FragmentTransaction fragmentTn = null;

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

        try {
            fragmentTn = fragmentMgr.beginTransaction();

            fragmentTn.replace(myFrameIds[0], entry.getFragment().asFragment(), entry.getTag());

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

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

    return lastTnId;
}

From source file:by.zatta.pilight.MainActivity.java

private void openFragment(BaseFragment mBaseFragment2) {
    Log.v(TAG, "openFragment");
    if (mBaseFragment2 != null) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        fm.popBackStack();// w  w w . j  a v a  2s.c om
        ft.replace(R.id.fragment_main, mBaseFragment2, mBaseFragment2.getName());
        ft.commit();
    }
}

From source file:com.android.mail.ui.TwoPaneController.java

@Override
public void showWaitForInitialization() {
    super.showWaitForInitialization();

    FragmentTransaction fragmentTransaction = mActivity.getFragmentManager().beginTransaction();
    fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    fragmentTransaction.replace(R.id.conversation_list_pane, getWaitFragment(), TAG_WAIT);
    fragmentTransaction.commitAllowingStateLoss();
}

From source file:cw.kop.autobackground.MainActivity.java

private void selectItem(int position, boolean slideAnimate) {

    if (position == currentPosition || position < 0) {
        return;//ww w  .ja v a 2  s  . c o m
    }

    currentPosition = position;

    setTitle(fragmentList[position]);
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

    if (slideAnimate) {
        fragmentTransaction.setCustomAnimations(R.animator.slide_from_left, R.animator.slide_to_right);
    }

    switch (position) {

    case 0:
        fragmentTransaction.replace(R.id.content_frame, sourceListFragment, "source_fragment").commit();
        break;
    case 1:
        fragmentTransaction.replace(R.id.content_frame, new WallpaperSettingsFragment()).commit();
        break;
    case 2:
        fragmentTransaction.replace(R.id.content_frame, new DownloadSettingsFragment()).commit();
        break;
    case 3:
        fragmentTransaction.replace(R.id.content_frame, new AccountSettingsFragment()).commit();
        break;
    case 4:
        fragmentTransaction.replace(R.id.content_frame, new EffectsSettingsFragment()).commit();
        break;
    case 5:
        fragmentTransaction.replace(R.id.content_frame, new NotificationSettingsFragment()).commit();
        break;
    case 6:
        fragmentTransaction.replace(R.id.content_frame, new WearSettingsFragment()).commit();
        break;
    case 7:
        fragmentTransaction.replace(R.id.content_frame, new AppSettingsFragment()).commit();
        break;
    case 8:
        fragmentTransaction.replace(R.id.content_frame, new ImageHistoryFragment()).commit();
        break;
    case 9:
        fragmentTransaction.replace(R.id.content_frame, new AboutFragment()).commit();
        break;
    default:

    }
}

From source file:cz.zcu.kiv.eeg.mobile.base.ui.NavigationActivity.java

public boolean openSection(int itemPosition) {
    Intent intent;//w  w w . j  av  a 2s . c o  m

    if (itemPosition != previousFragment) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        switch (itemPosition) {
        // dashboard
        case 0:
            DashboardFragment dashboardFrag;
            dashboardFrag = new DashboardFragment();
            fragmentTransaction.replace(R.id.content, dashboardFrag, DashboardFragment.TAG);
            fragmentTransaction.commit();
            previousFragment = itemPosition;
            break;

        // scenarios
        case 1:
            intent = new Intent();
            intent.setClass(this, ScenarioActivity.class);
            startActivity(intent);
            break;

        // experiments
        case 2:
            intent = new Intent();
            intent.setClass(this, ExperimentActivity.class);
            startActivity(intent);
            break;

        // datafile upload
        case 3:
            DataFileUploadFragment dataFileFrag;
            dataFileFrag = new DataFileUploadFragment();

            fragmentTransaction.replace(R.id.content, dataFileFrag, DataFileUploadFragment.TAG);
            fragmentTransaction.commit();
            previousFragment = itemPosition;
            break;

        // reservations
        case 4:
            ReservationFragment agendaFrag;
            agendaFrag = new ReservationFragment();

            fragmentTransaction.replace(R.id.content, agendaFrag, ReservationFragment.TAG);
            fragmentTransaction.commit();
            previousFragment = itemPosition;
            break;

        // application settings
        case 5:
            intent = new Intent();
            intent.setClass(this, SettingsActivity.class);
            startActivity(intent);
            break;
        default:
            return false;
        }
    }
    drawerLayout.closeDrawer(drawerList);
    return true;
}

From source file:com.abewy.android.apps.klyph.app.MainActivity.java

private void updateContent(int selection) {
    if (selection != oldSelection) {
        Bundle bundle = new Bundle();
        bundle.putString(KlyphBundleExtras.ELEMENT_ID, KlyphSession.getSessionUserId());

        String className = classes.get(selection);

        if (className.equals("com.abewy.android.apps.klyph.fragment.Chat")) {
            PackageManager pm = getPackageManager();

            try {
                pm.getPackageInfo(MESSENGER_PACKAGE_NAME, PackageManager.GET_ACTIVITIES);
                Intent intent = getPackageManager().getLaunchIntentForPackage(MESSENGER_PACKAGE_NAME);
                startActivity(intent);//from  ww w. j  ava2s  . c  om
            } catch (NameNotFoundException e) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(MESSENGER_PLAY_STORE_URI));
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
                startActivity(intent);
            }
        } else {
            if (selection < navAdapter.getCount())
                setTitle(navAdapter.getItem(selection));
            else
                setTitle(KlyphSession.getSessionUserName());

            Fragment fragment = Fragment.instantiate(MainActivity.this, className, bundle);

            if (previousFragment != null)
                previousFragment.onSetToBack(this);

            FragmentTransaction tx = getFragmentManager().beginTransaction();

            tx.replace(R.id.main, fragment, FRAGMENT_TAG);
            tx.commitAllowingStateLoss();
            ((IKlyphFragment) fragment).onSetToFront(this);

            previousFragment = (IKlyphFragment) fragment;

            navAdapter.setSelectedPosition(selection);
            navAdapter.notifyDataSetChanged();

            oldSelection = selection;

            if (notificationsFragment != null)
                notificationsFragment.setHasOptionsMenu(false);
        }
    }
}