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.example.scheme.MyPalettesActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    mCurrentPos = position;/*  w  w  w  .  ja  v a 2 s  .  c o  m*/
    Fragment fragment = new PaletteFragment();
    Bundle args = new Bundle();
    args.putInt(PaletteFragment.ARG_PALETTE_NUMBER, position);
    fragment.setArguments(args);

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

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

From source file:org.peterbaldwin.vlcremote.app.PickServerActivity.java

@SuppressWarnings("unchecked")
public <T extends android.app.Fragment> T findOrReplaceFragment(int res, String tag, Class<T> fragmentClass) {
    try {/*from w w w .  j a v a 2  s  .co m*/
        FragmentManager fm = getFragmentManager();
        T fragment = (T) fm.findFragmentByTag(tag);
        if (fragment == null) {
            fragment = fragmentClass.newInstance();
            FragmentTransaction fragmentTransaction = fm.beginTransaction();
            fragmentTransaction.replace(res, fragment, tag);
            fragmentTransaction.commit();
            fm.executePendingTransactions();
        }
        return fragment;
    } catch (InstantiationException e) {
        throw new IllegalArgumentException(e);
    } catch (IllegalAccessException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.breadwallet.presenter.activities.IntroActivity.java

private void showHideFragments(Fragment... fragments) {
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.hide(introWelcomeFragment);
    fragmentTransaction.hide(introNewRecoverFragment);
    fragmentTransaction.hide(introNewWalletFragment);
    fragmentTransaction.hide(introRecoverWalletFragment);
    fragmentTransaction.hide(introWarningFragment);
    for (Fragment f : fragments) {
        fragmentTransaction.show(f);//  w w w .j  a va2s .c  o m
    }
    fragmentTransaction.commitAllowingStateLoss();
}

From source file:com.royclarkson.springagram.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    this.menuPosition = position;
    String url = null;/*  www .ja va2 s  . c om*/
    Fragment fragment = null;
    String tag = null;
    switch (position) {
    case 0:
        url = getString(R.string.base_uri);
        fragment = HomeFragment.newInstance(url);
        tag = HomeFragment.TAG;
        break;
    case 1:
        if (this.apiResource != null) {
            url = this.apiResource.getLink(ApiResource.REL_ITEMS).getHref();
        }
        fragment = PhotoListFragment.newInstance(url);
        tag = PhotoListFragment.TAG;
        break;
    case 2:
        if (this.apiResource != null) {
            url = this.apiResource.getLink(ApiResource.REL_GALLERIES).getHref();
        }
        fragment = GalleryListFragment.newInstance(url);
        tag = GalleryListFragment.TAG;
        break;
    }
    // update the main content by replacing fragments
    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.popBackStack();
        fragmentManager.beginTransaction().replace(R.id.container, fragment, tag).commit();
    }
}

From source file:com.sharedcab.batchcar.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    //dont add them to back stack as these are all main pages

    FragmentManager fragmentManager = getFragmentManager();
    switch (position) {
    case 1:/*w w  w.j a v a2s . c om*/
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragmentManager.beginTransaction().replace(R.id.content_frame, bf).commit();
        break;
    case 2:
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragmentManager.beginTransaction().replace(R.id.content_frame, uf).commit();
        break;
    case 3:
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragmentManager.beginTransaction().replace(R.id.content_frame, prf).commit();
        break;
    default:
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragmentManager.beginTransaction().replace(R.id.content_frame, mainFragment).commit();
        break;
    }

    mDrawerList.setItemChecked(position, true);
    setTitle(mFrags[position]);
    mDrawerLayout.closeDrawer(mDrawerList);

}

From source file:com.jereksel.rommanager.MainActivity.java

public void SelectItem(final int possition) {

    rom = dataList.get(possition).getItemName();

    // Maybe this Thread is not necessary
    new Thread() {
        public void run() {

            Fragment fragment;/*from w  ww.jav  a2  s.c om*/
            final Bundle args = new Bundle();

            if (possition == 0) {
                fragment = new Status();
                //                    item.setVisible(false);
            } else {
                fragment = new ROMList();
                args.putString(ROMList.ROM_NAME, dataList.get(possition).getItemName());
            }

            fragment.setArguments(args);
            FragmentManager frgManager = getFragmentManager();

            frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

        }
    }.start();

    mDrawerList.setItemChecked(possition, true);
    setTitle(dataList.get(possition).getItemName());
    mDrawerLayout.closeDrawer(mDrawerList);

}

From source file:com.breadwallet.presenter.activities.IntroActivity.java

private void createFragments() {
    introWelcomeFragment = new IntroWelcomeFragment();
    introNewRecoverFragment = new IntroNewRecoverFragment();
    introNewWalletFragment = new IntroNewWalletFragment();
    introRecoverWalletFragment = new IntroRecoverWalletFragment();
    introWarningFragment = new IntroWarningFragment();

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    fragmentTransaction.add(R.id.intro_layout, introWelcomeFragment, IntroWelcomeFragment.class.getName());
    fragmentTransaction.add(R.id.intro_layout, introNewRecoverFragment,
            IntroNewRecoverFragment.class.getName());
    fragmentTransaction.add(R.id.intro_layout, introNewWalletFragment, IntroNewWalletFragment.class.getName());
    fragmentTransaction.add(R.id.intro_layout, introRecoverWalletFragment,
            IntroRecoverWalletFragment.class.getName());
    fragmentTransaction.add(R.id.intro_layout, introWarningFragment, IntroWarningFragment.class.getName());

    showHideFragments(introWelcomeFragment);
    newRecoverNoneFlag = 0;/* w  w  w. j av  a  2  s  .c o m*/
    fragmentTransaction.commitAllowingStateLoss();
}

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

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

    // set a custom shadow that overlays the main content when the drawer opens
    mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
    // set up the drawer's list view with items and click listener
    mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item,
            getResources().getStringArray(R.array.nav_items)));
    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

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

    // ActionBarDrawerToggle ties together the the proper interactions
    // between the sliding drawer and the action bar app icon
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            mDrawerLayout, /* 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 */
    ) {/*from  w  w w  .j a  v a 2s  .  c om*/
    };
    mDrawerLayout.setDrawerListener(mDrawerToggle);

    if (savedInstanceState == null) {
        mCurrentFragment = new CodersBestFragment(R.layout.home);
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, mCurrentFragment, "fragmentTag")
                .commit();

        Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {
            public void run() {
                selectItem(0);
            }
        }, 1000);
    } else {
        mCurrentFragment = getFragmentManager().findFragmentByTag("fragmentTag");
    }

    mDbAdapter = new CodersBFDatabaseAdapter(this);
    mDbAdapter.open();

    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);
    screenWidth = dm.widthPixels;
    screenHeight = dm.heightPixels;
}

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

public void startTimer(View view) {
    final Handler h = new Handler();
    final Context c = this;
    String timeText = ((EditText) findViewById(R.id.timerEditText)).getText().toString();
    if (!timerRunning) {
        if (!timeText.equals("")) {
            timerRunning = true;//from   ww w.  j  a v  a  2s .c om
            ((TextView) findViewById(R.id.timer_error_text)).setText("");
            final int time = Integer.parseInt(timeText);
            m_timerMenuItem.setTitle("" + time);
            Runnable r = new Runnable() {
                long m_startTime = System.currentTimeMillis();
                long m_endTime = m_startTime + 1000 * time;

                @Override
                public void run() {
                    if (System.currentTimeMillis() < m_endTime && timerRunning) {
                        // if the time hasn't elapsed
                        h.postDelayed(this, 1000);
                        // update the actionBar
                        m_timerMenuItem.setTitle(
                                Long.toString(time + ((m_startTime - System.currentTimeMillis()) / 1000)));
                    } else {
                        if (timerRunning) {
                            Toast.makeText(c, "Timer Done!", Toast.LENGTH_LONG).show();
                            Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
                            v.vibrate(500);
                        }
                        timerRunning = false;
                        m_timerMenuItem.setTitle("Set Timer");
                    }
                }
            };
            h.postDelayed(r, 1000);

            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(findViewById(R.id.timerEditText).getWindowToken(), 0);

            mCurrentFragment = new CodersBestFragment(R.layout.fragment_timer_stop);
            Bundle args = new Bundle();
            mCurrentFragment.setArguments(args);
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .replace(com.HACK.codersbestfriend.R.id.content_frame, mCurrentFragment, "fragmentTag")
                    .commit();
        } else {
            ((TextView) findViewById(R.id.timer_error_text)).setText("Enter a time!");
        }
    }
}

From source file:com.example.android.cloudnotes.ui.HomeActivity.java

/**
 * Callback from child fragment//from  w w w.  j  a va  2s .c  om
 */
public void onNoteDeleted() {
    // remove the NoteEditFragment after a deletion
    FragmentManager fm = getFragmentManager();
    NoteEditFragment edit = (NoteEditFragment) fm.findFragmentByTag(NOTE_EDIT_TAG);
    if (edit != null) {
        FragmentTransaction ft = fm.beginTransaction();
        ft.remove(edit);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.commit();
    }
}