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.nearalias.musicplayer.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    if (getFragmentManager().findFragmentById(R.id.content_frame) == null) {
        Fragment fragment = new PlayListFragment();
        Bundle args = new Bundle();
        args.putInt(PlayListFragment.ARG_PLANET_NUMBER, position);
        makeUpFakeSongs();//ww  w  .  j av a  2s .  co m
        args.putStringArrayList(PlayListFragment.ARG_SONG_NAME, names);
        args.putStringArrayList(PlayListFragment.ARG_SONG_ARTIST, artists);
        args.putStringArrayList(PlayListFragment.ARG_SONG_ALBUM, albums);
        args.putStringArrayList(PlayListFragment.ARG_SONG_GENRE, genres);
        args.putStringArrayList(PlayListFragment.ARG_SONG_LYRIC, lyrics);
        fragment.setArguments(args);

        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    }
    // update selected item and title, then close the drawer
    drawerListView.setItemChecked(position, true);
    setTitle(planetTitles[position]);
    drawerLayout.closeDrawer(drawerListView);
}

From source file:com.imobilize.blogposts.activities.MainActivity.java

private void displayView(int position) {
    // update the main content by replacing fragments
    Fragment fragment = null;/*from  w w w. j  a v  a2  s . c om*/
    switch (position) {
    case 0:
        fragment = new HomeFragment();
        break;
    case 1:
        fragment = new SubscribeFragment();
        break;
    case 2:
        fragment = new InfoFragment();
        break;
    default:
        break;
    }

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

        // update selected item and title, then close the drawer
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        setTitle(navMenuTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        Log.e("MainActivity", "Error in creating fragment");
    }
}

From source file:me.pzheng.conn.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    FragmentManager fm = getFragmentManager();
    FragmentTransaction transaction = fm.beginTransaction();

    // debug code
    // Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();

    Fragment fragment;/* ww  w.  j a  va 2s  .c  om*/
    switch (position) {
    case 0: //connect
        fragment = fm.findFragmentByTag(ConnectFragment.TAG);
        if (fragment == null) {
            fragment = new ConnectFragment();
        }
        transaction.replace(R.id.container, fragment, ConnectFragment.TAG);
        transaction.addToBackStack(null);
        transaction.commit();
        break;
    case 1: //settings
        /* fragment = getFragmentManager().findFragmentByTag(StatsFragment.TAG);
        if (fragment == null) {
            fragment = new StatsFragment();
        }
        getFragmentManager().beginTransaction().replace(R.id.container, fragment, StatsFragment.TAG).commit(); */
        fragment = fm.findFragmentByTag(SettingsFragment.TAG);
        if (fragment == null) {
            fragment = new SettingsFragment();
        }
        transaction.replace(R.id.container, fragment, SettingsFragment.TAG);
        transaction.addToBackStack(null);
        transaction.commit();
        break;
    case 2: //about
        fragment = fm.findFragmentByTag(AboutFragment.TAG);
        if (fragment == null) {
            fragment = new AboutFragment();
        }
        transaction.replace(R.id.container, fragment, AboutFragment.TAG);
        transaction.addToBackStack(null);
        transaction.commit();
        break;
    }

    //transaction.commit();

}

From source file:com.cloudbees.gasp.activity.ReviewActivity.java

private void addGaspFragments() {
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    mGaspReviewFragment = new GaspReviewFragment() {
        @Override//from   ww  w.j a v a2 s  .c  o  m
        public void onSuccess(String location) {
            try {
                Log.d(TAG, "Gasp! review added: " + location);
                addGaspEvent(mPlacesReference, new URL(location));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onFailure() {
            Log.e(TAG, "Error adding Gasp! review");
        }
    };
    mAddEventFragment = new AddEventFragment() {
        @Override
        public void onSuccess(EventResponse eventResponse) {
            Log.d(TAG, "Event added: " + eventResponse.getEvent_id());
        }

        @Override
        public void onFailure(String status) {
            Log.d(TAG, "Error adding event to Google Places API");
        }
    };

    ft.add(mGaspReviewFragment, "Gasp Review Fragment");
    ft.add(mAddEventFragment, "Add Event Fragment");
    ft.commit();
}

From source file:it.polimi.spf.app.MainActivity.java

@Override
public void onItemSelect(int position, boolean replace) {
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    mTitle = getPageTitle(position);//www  .  j  a v a  2  s.  c  o  m
    if (replace) {
        fragmentManager.beginTransaction().replace(R.id.container, createFragment(position)).commit();
    }
    invalidateOptionsMenu();
}

From source file:org.chameleonos.firewall.FirewallActivity.java

/** Swaps fragments in the main content view */
private void selectItem(int position) {
    // Create a new fragment and specify the planet to show based on position
    Fragment fragment = null;/*  w  w  w .ja v  a2s .  com*/
    try {
        fragment = (Fragment) (Class.forName(mNavItems.get(position).fragment)).newInstance();
    } catch (InstantiationException e) {
    } catch (IllegalAccessException e) {
    } catch (ClassNotFoundException e) {
    }

    if (fragment != null) {
        // Insert the fragment by replacing any existing fragment
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
        setTitle(mNavItems.get(position).title);
    }

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

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

private void selectItem(int position) {

    Fragment fragments[] = { new Profile(), new Home(), new Notification(), new Home(), new Achievement(),
            new Settings() };
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragments[position]).commit();

    // update selected item and title, then close the drawer
    mDrawerList.setItemChecked(position, true);
    setTitle(menuItems[position]);//  w w  w .ja  v a  2  s . c  o  m
    mDrawerLayout.closeDrawer(mDrawerList);
}

From source file:com.translationexchange.samples.wammer.activities.MainActivity.java

/**
 * Diplaying fragment view for selected nav drawer list item
 * *//*from w  w w  .j ava 2 s.co  m*/
private void displayView(int position) {
    // update the main content by replacing fragments

    NavDrawerItem item = getNavDrawerItems().get(position);
    Fragment fragment = item.getFragment();

    if (fragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
        currentViewTitle = item.getTitle();
        getActionBar().setTitle(Tml.translate(currentViewTitle));

        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        mDrawerLayout.closeDrawer(mDrawerList);
    } else {
        // error in creating fragment
        // Log.e("MainActivity", "Error in creating fragment");
    }
}

From source file:com.grarak.romswitcher.MoreSettingsActitvity.java

private void selectItem(int position) {
    Fragment fragment = null;//from  www  .j a v a2s . c  om
    // update the main content by replacing fragments
    switch (position) {
    case 0:
        fragment = new GeneralFragment();
        break;
    case 1:
        fragment = new SettingsFragment();
        break;
    case 2:
        fragment = new AboutUsFragment();
    default:
        break;
    }
    Bundle args = new Bundle();
    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(mMenu[position]);
    mDrawerLayout.closeDrawer(mDrawerList);
}

From source file:curiosamente.com.app.activities.main.MainActivity.java

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

    FragmentManager fm = getFragmentManager();
    FragmentTransaction fragmentTransaction = fm.beginTransaction();
    MainFragment mainFragment = new MainFragment();
    fragmentTransaction.add(R.id.main_layout, mainFragment);
    fragmentTransaction.commit();//w ww .j a  va  2 s  . com

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {

            Bar[] bars = (Bar[]) intent.getExtras().get(HttpService.RETURNOBJECT_EXTRA_PROPERTY);
            //                FragmentManager fm = getFragmentManager();
            //                FragmentTransaction fragmentTransaction = fm.beginTransaction();
        }
    };

    mDrawerLayout = (DrawerLayout) findViewById(R.id.activity_main);
    mDrawerTextView = (TextView) findViewById(R.id.facebook_name_text);
    mDrawerLogoutButton = (Button) mDrawerLayout.findViewById(R.id.facebook_logout_button);
    mDrawerImage = (ImageView) findViewById(R.id.profileImage);

    Intent intent = new Intent(this, HttpService.class);
    intent.putExtra(HttpService.URL_EXTRA_PROPERTY, "http://192.168.0.13:8080/bar");
    intent.putExtra(HttpService.CLASS_EXTRA_PROPERTY, Bar[].class);
    startService(intent);

    initDrawer();
}