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.tassadar.multirommgr.MainActivity.java

/** Swaps fragments in the main content view */
private void selectItem(int position) {
    if (position < 0 || position >= m_fragments.length) {
        Log.e("MultiROMMgr", "Invalid fragment index " + position);
        return;//from   ww  w.ja v  a 2s  .co m
    }

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction t = fragmentManager.beginTransaction();

    if (m_curFragment != -1)
        t.hide(m_fragments[m_curFragment]);
    t.show(m_fragments[position]);
    t.commit();

    m_curFragment = position;

    // Highlight the selected item, update the title, and close the drawer
    m_drawerList.setItemChecked(position, true);
    setTitle(m_fragmentTitles[position]);
    m_drawerLayout.closeDrawer(m_drawerList);
}

From source file:blackman.matt.infinitebrowser.InfinityBrowser.java

/**
 * Replaces the item fragments in the navigation drawer when a new item is selected.
 * @param position the position of the newly selected item
 *///www . j av  a 2  s  .co  m
@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
            .commit();
    setTitle(mTitle);
}

From source file:com.android.calendar.SearchActivity.java

private void showEventInfo(EventInfo event) {
    if (mShowEventDetailsWithAgenda) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction ft = fragmentManager.beginTransaction();

        mEventInfoFragment = new EventInfoFragment(this, event.id, event.startTime.toMillis(false),
                event.endTime.toMillis(false), event.getResponse(), false,
                EventInfoFragment.DIALOG_WINDOW_STYLE, null /* No reminders to explicitly pass in. */);
        ft.replace(R.id.agenda_event_info, mEventInfoFragment);
        ft.commit();/*from w w w. ja v a 2s .  c o m*/
    } else {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri eventUri = ContentUris.withAppendedId(Events.CONTENT_URI, event.id);
        intent.setData(eventUri);
        intent.setClass(this, EventInfoActivity.class);
        intent.putExtra(EXTRA_EVENT_BEGIN_TIME, event.startTime != null ? event.startTime.toMillis(true) : -1);
        intent.putExtra(EXTRA_EVENT_END_TIME, event.endTime != null ? event.endTime.toMillis(true) : -1);
        startActivity(intent);
    }
    mCurrentEventId = event.id;
}

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

private void selectItem(int position) {
    // update the main content by replacing fragments
    mDrawerLayout.closeDrawer(mDrawerList);
    //      Bundle args=null;
    fragment = new DynamicStateFragment();
    if (position == 6) {
        fragment = new SnowBallFriendsFragment(height, width);
    }/* ww  w  .  ja  v a 2  s. c  o  m*/
    //      args = new Bundle();
    //      args.putInt(String.valueOf(position), position);
    //      fragment.setArguments(args);
    mTitle = mPlanetTitles[position];
    switch (position) {
    case 1:
        mTitle = "";
        break;
    }

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    //      fragmentManager.;
    setTitle(mTitle);
}

From source file:ee.siimplangi.ilmaprognoos.MainActivity.java

private void selectItem(int position) {
    try {/*w ww  .ja v  a2 s  .  c  o m*/
        selectedItem = position;
        WeatherFragment fragment = new WeatherFragment();
        fragment.setRetainInstance(false);
        FragmentManager fragmentManager = getFragmentManager();
        Bundle args = new Bundle();
        args.putParcelable(WeatherFragment.FORECAST_KEY, forecastData.get(position));
        fragment.setArguments(args);
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commitAllowingStateLoss();
        setTitle(availableDates.get(position));
        mDrawerList.setItemChecked(position, true);
        mDrawerLayout.closeDrawer(mDrawerList);
    } catch (IllegalStateException e) {
        Log.d(TAG, "Expected error", e);
        // If we received the XML results but the savedInstanceState was already called.
        // This happened for me when I launched the application from android studio, but the screen was locked

    }
}

From source file:com.sawyer.advadapters.app.adapters.jsonadapter.UnitTestJSONArrayActivity.java

@Override
protected void initFrags() {
    super.initFrags();
    FragmentManager manager = getFragmentManager();
    mListFragment = (UnitTestJSONArrayFragment) manager.findFragmentByTag(TAG_ADAPTER_FRAG);
    if (mListFragment == null) {
        mListFragment = UnitTestJSONArrayFragment.newInstance();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.frag_container, mListFragment, TAG_ADAPTER_FRAG);
        transaction.commit();/* www .ja v  a2  s  .  com*/
    }
}

From source file:it.gmariotti.cardslib.demo.extras.MainActivity.java

private void openDialogFragment(DialogFragment dialogStandardFragment) {
    if (dialogStandardFragment != null) {
        FragmentManager fm = getFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        Fragment prev = fm.findFragmentByTag("carddemo_dialog_extras");
        if (prev != null) {
            ft.remove(prev);//from   www .  j a  v a  2s.  c  o  m
        }
        //ft.addToBackStack(null);

        dialogStandardFragment.show(ft, "carddemo_dialog_extras");
    }
}

From source file:com.HACK.codersbestfriend.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    Log.i("ITEM", position + "");
    if (mCurrentFragment instanceof CodersBestFragment) {
        if (((CodersBestFragment) mCurrentFragment).getResource() == R.layout.activity_design) {
            ((DrawPanel) (findViewById(R.id.drawPanel))).closing(this);
        }/* w ww.j  a  va2 s .co  m*/
    }
    switch (position) {
    case 0: // Tasks
        mCurrentFragment = new TaskViewFragment();
        break;
    case 1: // Design
        mCurrentFragment = new CodersBestFragment(R.layout.activity_design);
        //((CodersBestFragment)mCurrentFragment).setPaths(paths);
        //((CodersBestFragment)mCurrentFragment).setBackupPaths(paths);
        break;
    case 2: // Timer
        if (timerRunning) {
            mCurrentFragment = new CodersBestFragment(R.layout.fragment_timer_stop);
            break;
        }
        mCurrentFragment = new CodersBestFragment(R.layout.fragment_timer);
        break;
    default:
        mCurrentFragment = new CodersBestFragment(R.layout.activity_main);
    }
    Bundle args = new Bundle();
    args.putInt(ARG_VIEW_NUMBER, position);
    mCurrentFragment.setArguments(args);

    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, mCurrentFragment, "fragmentTag").commit();

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

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

/**
 * Use your own layout for the panes./*from  w w w  .ja  v  a  2s . 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.richtodd.android.quiltdesign.app.ThemeEditActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_theme_edit);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    m_themeNameArgument = getIntent().getStringExtra(ARG_THEME_NAME);

    if (savedInstanceState != null) {
        m_saveAsThemeName = savedInstanceState.getString("saveAsThemeName");
    }/*from  w  w  w .j av  a2  s.  c  o m*/

    ActionBar ab = getActionBar();
    ab.setTitle("Edit Theme");
    ab.setSubtitle(getCurrentThemeName());

    FragmentManager fm = getFragmentManager();

    // Create color settings fragment.
    {
        Fragment f = fm.findFragmentById(R.id.layout_themeEditFragment);
        if (f == null) {
            f = ThemeEditFragment.create(m_themeNameArgument);
            fm.beginTransaction().add(R.id.layout_themeEditFragment, f).commit();
        }
    }
}