Example usage for android.app FragmentManager beginTransaction

List of usage examples for android.app FragmentManager beginTransaction

Introduction

In this page you can find the example usage for android.app FragmentManager beginTransaction.

Prototype

public abstract FragmentTransaction beginTransaction();

Source Link

Document

Start a series of edit operations on the Fragments associated with this FragmentManager.

Usage

From source file:com.ppgllrd.alfil.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d("ppgllrd", "onCreate: " + savedInstanceState);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_double);

    DrawerLayout drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    ListView drawerList = (ListView) findViewById(R.id.drawer_list);

    Course[] courses = Course.getCourses();
    String currentYear = "";
    drawerItems = new ArrayList<DrawerItem>();
    for (Course course : courses) {
        if (course.getYear().compareTo(currentYear) != 0) {
            currentYear = course.getYear();
            drawerItems.add(new DrawerSection(currentYear));
        }//ww w .j ava2s. com
        drawerItems.add(new DrawerCourse(course));
    }

    // set up the drawer's list view with items and click listener
    drawerList.setAdapter(new DrawerAdapter(this, R.layout.drawer_course_item, drawerItems));

    drawerList.setOnItemClickListener(new DrawerItemClickListener());

    // enable ActionBar app icon to behave as action to toggle nav drawer
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);

    actionBarTitleController = new ActionBarTitleController(this, /* host Activity */
            drawerLayout, /* DrawerLayout object */
            R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open, /* "open drawer" description for accessibility */
            R.string.drawer_close /* "close drawer" description for accessibility */
    );

    FragmentManager fm = getFragmentManager();

    drawerSelectedIdx = 1; // start by selecting first course
    if (fm.findFragmentByTag(StudentsListFragment.FragmentTag) == null) {
        DrawerCourse drawerCourse = (DrawerCourse) drawerItems.get(drawerSelectedIdx);
        studentsListFragment = new StudentsListFragment();
        Bundle args = new Bundle();
        args.putParcelable(StudentsListFragment.ARG_GROUP_STUDENTS_COURSE, drawerCourse.getCourse());
        studentsListFragment.setArguments(args);

        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.list_Fragment_Placeholder, studentsListFragment, StudentsListFragment.FragmentTag);
        ft.commit();
    } else {
        studentsListFragment = (StudentsListFragment) getFragmentManager()
                .findFragmentByTag(StudentsListFragment.FragmentTag);
    }

    if (fm.findFragmentByTag(StudentInfoFragment.FragmentTag) == null) {
        studentInfoFragment = new StudentInfoFragment();
        Bundle args = new Bundle();
        args.putParcelable(StudentInfoFragment.ARG_STUDENT,
                new Student(((DrawerCourse) drawerItems.get(drawerSelectedIdx)).getCourse()));
        studentInfoFragment.setArguments(args);

        FragmentTransaction ft = fm.beginTransaction();
        ft.add(R.id.info_Fragment_Placeholder, studentInfoFragment, StudentInfoFragment.FragmentTag);
        ft.commit();
    } else {
        studentInfoFragment = (StudentInfoFragment) getFragmentManager()
                .findFragmentByTag(StudentInfoFragment.FragmentTag);
    }

    FragmentTransaction ft = fm.beginTransaction();
    ft.hide(studentInfoFragment);
    ft.show(studentsListFragment);
    //ft.addToBackStack(null);
    ft.commit();

    // ActivitySwipeDetector activitySwipeDetector = new ActivitySwipeDetector(this);
    // DrawerLayout lowestLayout = (DrawerLayout)this.findViewById(R.id.drawer_layout);
    // lowestLayout.setOnTouchListener(activitySwipeDetector);

}

From source file:com.github.chenxiaolong.dualbootpatcher.MainActivity.java

private void hideFragments(boolean animate) {
    FragmentManager fm = getFragmentManager();

    Fragment prevRoms = fm.findFragmentByTag(SwitcherListFragment.TAG);
    Fragment prevPatchFile = fm.findFragmentByTag(PatchFileFragment.TAG);
    Fragment prevFreeSpace = fm.findFragmentByTag(FreeSpaceFragment.TAG);
    Fragment prevAbout = fm.findFragmentByTag(AboutFragment.TAG);

    FragmentTransaction ft = fm.beginTransaction();

    if (animate) {
        ft.setCustomAnimations(0, R.animator.fragment_out);
    }//from w  w w .  j a  v  a2s. co m

    if (prevRoms != null) {
        ft.hide(prevRoms);
    }
    if (prevPatchFile != null) {
        ft.hide(prevPatchFile);
    }
    if (prevFreeSpace != null) {
        ft.hide(prevFreeSpace);
    }
    if (prevAbout != null) {
        ft.hide(prevAbout);
    }

    ft.commit();
    fm.executePendingTransactions();
}

From source file:com.bradbergeron.splitviewcontrollerdemo.activities.MainActivity.java

@Override
public void onDrawerItemSelected(final DrawerFragment.FragmentDrawerItem fragmentDrawerItem) {
    if (mDrawerLayout.isDrawerOpen(Gravity.START)) {
        mDrawerLayout.closeDrawer(Gravity.START);
    }/*from   w w  w.j  a va 2  s  .  c  om*/

    final FragmentManager fragmentManager = getFragmentManager();
    final String fragmentClassName = fragmentDrawerItem.getFragmentClass().getName();

    Fragment newFragment = fragmentManager.findFragmentByTag(fragmentClassName);

    if (newFragment == null) {
        newFragment = Fragment.instantiate(this, fragmentClassName);
    } else if (newFragment.isAdded()) {
        return;
    } else if (fragmentDrawerItem.isDefaultItem() && fragmentManager.getBackStackEntryCount() > 0) {
        fragmentManager.popBackStack("Drawer", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }

    final FragmentTransaction transaction = fragmentManager.beginTransaction();
    transaction.replace(R.id.content, newFragment, fragmentClassName);

    if (!fragmentDrawerItem.isDefaultItem()) {
        transaction.addToBackStack("Drawer");
    }

    transaction.commit();
}

From source file:de.sourcestream.movieDB.controller.SearchList.java

public void onSuggestionClick(int id, String mediaType, String title) {
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    boolean result;
    Bundle args;//from w w w.j  a  v a  2  s  . c o  m
    switch (mediaType) {
    case "movie":
        activity.setRestoreMovieDetailsAdapterState(true);
        activity.setRestoreMovieDetailsState(false);
        if (movieDetails != null && lastVisitedId == id && movieDetails.getTimeOut() == 0) {
            // Old movie details retrieve info and re-init component else crash
            movieDetails.onSaveInstanceState(new Bundle());
            Bundle bundle = new Bundle();
            bundle.putInt("id", id);
            Bundle save = movieDetails.getSave();
            movieDetails = new MovieDetails();
            movieDetails.setTimeOut(0);
            movieDetails.setSave(save);
            movieDetails.setArguments(bundle);
        } else
            movieDetails = new MovieDetails();

        args = new Bundle();
        args.putInt("id", id);
        movieDetails.setArguments(args);

        movieDetails.setTitle(title);
        transaction.replace(R.id.frame_container, movieDetails);
        result = true;
        break;

    case "person":
        activity.setRestoreMovieDetailsAdapterState(true);
        activity.setRestoreMovieDetailsState(false);
        if (castDetails != null && lastVisitedId == id && castDetails.getTimeOut() == 0) {
            // Old movie details retrieve info and re-init component else crash
            castDetails.onSaveInstanceState(new Bundle());
            Bundle bundle = new Bundle();
            bundle.putInt("id", id);
            Bundle save = castDetails.getSave();
            castDetails = new CastDetails();
            castDetails.setTimeOut(0);
            castDetails.setSave(save);
            castDetails.setArguments(bundle);
        } else
            castDetails = new CastDetails();

        args = new Bundle();
        args.putInt("id", id);
        castDetails.setArguments(args);

        castDetails.setTitle(title);
        transaction.replace(R.id.frame_container, castDetails);
        result = true;
        break;

    case "tv":
        activity.setRestoreMovieDetailsAdapterState(true);
        activity.setRestoreMovieDetailsState(false);
        if (tvDetails != null && lastVisitedId == id && tvDetails.getTimeOut() == 0) {
            // Old movie details retrieve info and re-init component else crash
            tvDetails.onSaveInstanceState(new Bundle());
            Bundle bundle = new Bundle();
            bundle.putInt("id", id);
            Bundle save = tvDetails.getSave();
            tvDetails = new TVDetails();
            tvDetails.setTimeOut(0);
            tvDetails.setSave(save);
            tvDetails.setArguments(bundle);
        } else
            tvDetails = new TVDetails();

        args = new Bundle();
        args.putInt("id", id);
        tvDetails.setArguments(args);

        tvDetails.setTitle(title);
        transaction.replace(R.id.frame_container, tvDetails);
        result = true;
        break;

    default:
        result = false;
        break;
    }

    lastVisitedId = id;

    if (result) {
        // add the current transaction to the back stack:
        transaction.addToBackStack("movieList");
        transaction.commit();
        // collapse the search View
        ((MainActivity) getActivity()).collapseSearchView();
    }

}

From source file:com.example.volunteerhandbook.MainActivity.java

private void selectItem(int position) {
    if (mPassword == null || mPassword.length() < 4) {
        checkPassword();/*from   w ww. j a  v  a 2  s  .  co  m*/
        return;
    }
    if (noDrawYet)
        return;
    if (position == mPageTitles.length - 1) {
        closeSpeaker();
        finish();
    }
    /*
    <item>0??</item>
    <item>1?</item>
    <item>2?</item>
    <item>3???</item>
    <item>4?</item>
    <item>5</item>        
    <item>6?</item>
    <item>7</item>
    <item>8</item>
    <item>9</item>
    <item>10</item>
    <item>11</item>
    <item>12??</item>
    */
    MenuItem.OnMenuItemClickListener aListener;
    Fragment fragment = null;
    ListRecordBase aList = null;
    Bundle args = new Bundle();
    switch (position) {
    case 0:
    case 1:
        openAgendaPage(null);
        return;
    case 2:
    case 3:
    case 4:
        setUpMyAgenda("Create New");
        return;
    case 6:
        openForumChatPage(null);
        return;
    case 10:
        openCommitmentPage(null);
        return;
    case 11:
        openPersonalPage(null);
        return;

    case 9:
    case 8:
    case 5:
        openMemoPage(null);
        return;

    case 7:
        aList = new VisitRecord();
        break;

    default:
        return;
    }

    args.putString(PAGE_TITLE, mPages[position]);
    aListener = aList;
    //String name=getResources().getStringArray(R.array.page_array_EN)[position];
    args.putInt(ListRecords.PARENT_PASSDOWN_KEY, position);
    fragment = (Fragment) aList;

    fragment.setArguments(args);
    mCurrentFragment = fragment;
    mCurrentPage = position;
    for (int i = 0; i < mActionMenu.size(); i++) {
        mActionMenu.getItem(i).setOnMenuItemClickListener(aListener);
    }
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.main_content_frame, fragment).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(mPageTitles[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

From source file:de.sourcestream.movieDB.controller.SearchList.java

/**
 * Callback method to be invoked when an item in this AdapterView has been clicked.
 *
 * @param parent   The AdapterView where the click happened.
 * @param view     The view within the AdapterView that was clicked (this will be a view provided by the adapter)
 * @param position The position of the view in the adapter.
 * @param id       The row id of the item that was clicked.
 *//*from  www .j av a 2 s.  co  m*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    FragmentManager manager = getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    boolean result;
    Bundle args;
    switch (searchList.get(position).getMediaType()) {
    case "movie":
        activity.setRestoreMovieDetailsAdapterState(true);
        activity.setRestoreMovieDetailsState(false);
        if (movieDetails != null && lastVisitedId == searchList.get(position).getId()
                && movieDetails.getTimeOut() == 0) {
            // Old movie details retrieve info and re-init component else crash
            movieDetails.onSaveInstanceState(new Bundle());
            Bundle bundle = new Bundle();
            bundle.putInt("id", searchList.get(position).getId());
            Bundle save = movieDetails.getSave();
            movieDetails = new MovieDetails();
            movieDetails.setTimeOut(0);
            movieDetails.setSave(save);
            movieDetails.setArguments(bundle);
        } else
            movieDetails = new MovieDetails();

        args = new Bundle();
        args.putInt("id", searchList.get(position).getId());
        movieDetails.setArguments(args);

        movieDetails.setTitle(searchList.get(position).getTitle());
        transaction.replace(R.id.frame_container, movieDetails);
        result = true;
        break;

    case "person":
        activity.setRestoreMovieDetailsAdapterState(true);
        activity.setRestoreMovieDetailsState(false);
        if (castDetails != null && lastVisitedId == searchList.get(position).getId()
                && castDetails.getTimeOut() == 0) {
            // Old movie details retrieve info and re-init component else crash
            castDetails.onSaveInstanceState(new Bundle());
            Bundle bundle = new Bundle();
            bundle.putInt("id", searchList.get(position).getId());
            Bundle save = castDetails.getSave();
            castDetails = new CastDetails();
            castDetails.setTimeOut(0);
            castDetails.setSave(save);
            castDetails.setArguments(bundle);
        } else
            castDetails = new CastDetails();

        args = new Bundle();
        args.putInt("id", searchList.get(position).getId());
        castDetails.setArguments(args);

        castDetails.setTitle(searchList.get(position).getTitle());
        transaction.replace(R.id.frame_container, castDetails);
        result = true;
        break;

    case "tv":
        activity.setRestoreMovieDetailsAdapterState(true);
        activity.setRestoreMovieDetailsState(false);
        if (tvDetails != null && lastVisitedId == searchList.get(position).getId()
                && tvDetails.getTimeOut() == 0) {
            // Old movie details retrieve info and re-init component else crash
            tvDetails.onSaveInstanceState(new Bundle());
            Bundle bundle = new Bundle();
            bundle.putInt("id", searchList.get(position).getId());
            Bundle save = tvDetails.getSave();
            tvDetails = new TVDetails();
            tvDetails.setTimeOut(0);
            tvDetails.setSave(save);
            tvDetails.setArguments(bundle);
        } else
            tvDetails = new TVDetails();

        args = new Bundle();
        args.putInt("id", searchList.get(position).getId());
        tvDetails.setArguments(args);

        tvDetails.setTitle(searchList.get(position).getTitle());
        transaction.replace(R.id.frame_container, tvDetails);
        result = true;
        break;

    default:
        result = false;
        break;
    }

    lastVisitedId = searchList.get(position).getId();

    if (result) {
        // add the current transaction to the back stack:
        transaction.addToBackStack("movieList");
        transaction.commit();
        // collapse the search View
        ((MainActivity) getActivity()).collapseSearchView();
    }

}

From source file:geeshang.nasaimage.MainActivity.java

private void downloadOrReload() {
    FragmentManager fragmentManager = getFragmentManager();
    dataReloader = (DataReloader) fragmentManager.findFragmentByTag("dataReloader");
    //To prevent MainActivity restart from rotating the screen,
    //if dataReloader is null, download the data and store in the DataReloader.
    if (dataReloader == null) {
        download();/*from  w  w  w .  ja  v a2 s  .  c o  m*/
        dataReloader = new DataReloader();
        fragmentManager.beginTransaction().add(dataReloader, "dataReloader").commit();
        dataReloader.setData(this);
    } else {
        title_data = dataReloader.getData().title_data;
        date_data = dataReloader.getData().date_data;
        image_url = dataReloader.getData().image_url;
        image_data = dataReloader.getData().image_data;
        dayIndex = dataReloader.getData().dayIndex;
        description_data = dataReloader.getData().description_data;
        setProgressBarGone();
        resetView();
    }
}

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 ww w  .  j  a  v  a2  s . 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.evandroid.musica.MainLyricActivity.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();
    if (!lyricsViewFragment.isAdded()) {
        fragmentTransaction.add(R.id.main_fragment_container, lyricsViewFragment, LYRICS_FRAGMENT_TAG);
    }/*from  www .j av  a 2  s. c om*/

    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:com.lgallardo.qbittorrentclient.ItemstFragment.java

private void newDetailsFragment(int position) {

    detailsFragment = new TorrentDetailsFragment();

    // Get torrent from MainActivity
    detailsFragment.setTorrent(MainActivity.lines[position]);

    detailsFragment.setPosition(position);

    if (detailsFragment != null) {
        FragmentManager fragmentManager = getFragmentManager();

        if (getActivity().findViewById(R.id.one_frame) != null) {
            fragmentManager.beginTransaction()
                    .replace(this.getSecondFragmentContainer(), detailsFragment, "firstFragment")
                    .addToBackStack("secondFragment").commit();

            // Change toolbar home button behaviour
            originalListener = MainActivity.drawerToggle.getToolbarNavigationClickListener();

            MainActivity.drawerToggle.setDrawerIndicatorEnabled(false);
            MainActivity.drawerToggle.setHomeAsUpIndicator(R.drawable.ic_drawer);
            MainActivity.drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
                @Override//  w  ww  .j  a  v  a 2s.  c om
                public void onClick(View v) {

                    // Disable refreshing
                    MainActivity.disableRefreshSwipeLayout();

                    // Set default toolbar behaviour
                    ((MainActivity) getActivity()).getSupportActionBar().setDisplayShowTitleEnabled(true);
                    MainActivity.drawerToggle.setDrawerIndicatorEnabled(true);
                    MainActivity.drawerToggle.setToolbarNavigationClickListener(originalListener);
                    ((MainActivity) getActivity()).setSelectionAndTitle(MainActivity.currentState);

                    // Show herderInfo in phone's view
                    if (getActivity().findViewById(R.id.one_frame) != null) {

                        if (MainActivity.headerInfo != null) {
                            if (MainActivity.header) {
                                MainActivity.headerInfo.setVisibility(View.VISIBLE);
                            } else {
                                MainActivity.headerInfo.setVisibility(View.GONE);
                            }
                        }

                    }

                    FragmentManager fm = getFragmentManager();

                    fm.popBackStack();

                }
            });
        } else {
            fragmentManager.beginTransaction()
                    .replace(this.getSecondFragmentContainer(), detailsFragment, "secondFragment")
                    .addToBackStack("secondFragment").commit();
        }
    }

}