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:org.onepf.opfiab.ComponentIabHelper.java

ComponentIabHelper(@Nullable final android.support.v4.app.FragmentManager supportFragmentManager,
        @Nullable final android.app.FragmentManager fragmentManager) {
    super();//from www .  j  a  v  a2s .  co  m
    // Register for lifecycle event right a way
    OPFIab.register(this);

    if (supportFragmentManager != null) {
        OPFLog.d("ComponentIabHelper uses android.support.v4.app.Fragment.");
        final android.support.v4.app.Fragment existingFragment = supportFragmentManager
                .findFragmentByTag(FRAGMENT_TAG);
        if (existingFragment != null) {
            // Fragment already attached
            opfFragment = existingFragment;
            register();
            return;
        }
        final android.support.v4.app.Fragment fragment = OPFIabSupportFragment.newInstance();
        opfFragment = fragment;
        // Attach new fragment
        supportFragmentManager.beginTransaction().add(fragment, FRAGMENT_TAG).commit();
        // wait for onAttach() callback
        supportFragmentManager.executePendingTransactions();
        return;
    }

    if (fragmentManager != null) {
        OPFLog.d("ComponentIabHelper uses android.app.Fragment.");
        final Fragment existingFragment = fragmentManager.findFragmentByTag(FRAGMENT_TAG);
        if (existingFragment != null) {
            opfFragment = existingFragment;
            register();
            return;
        }
        final Fragment fragment = OPFIabFragment.newInstance();
        opfFragment = fragment;
        fragmentManager.beginTransaction().add(fragment, FRAGMENT_TAG).commit();
        fragmentManager.executePendingTransactions();
        return;
    }

    throw new IllegalStateException();
}

From source file:de.theknut.xposedgelsettings.ui.MainActivity.java

private void selectItem(final int position) {

    // update the main content by replacing fragments
    FragmentManager fm = getFragmentManager();

    switch (position) {
    case 0:/* w  ww .ja  v a 2s . co m*/
        mCurrFragment = new FragmentWelcome();
        break;
    case 1:
        mCurrFragment = new FragmentGeneral();
        break;
    case 2:
        mCurrFragment = new FragmentSearchbar();
        break;
    case 3:
        mCurrFragment = new FragmentHomescreen();
        break;
    case 4:
        mCurrFragment = new FragmentAppDrawer();
        break;
    case 5:
        mCurrFragment = new FragmentGestures();
        break;
    case 6:
        mCurrFragment = new FragmentNotificationBadges();
        break;
    case 7:
        mCurrFragment = new FragmentIcon();
        break;
    case 8:
        mCurrFragment = new FragmentAndroidIntegration();
        break;
    case 9:
        mCurrFragment = new FragmentBackupRestore();
        break;
    case 10:
        mCurrFragment = new FragmentSettings();
        break;
    // !!!! don't forget to change onActivityResult, too !!!!
    case 11:
        mCurrFragment = new FragmentPremium();
        break;
    case 12:
        mCurrFragment = new FragmentReverseEngineering();
        break;
    }

    fm.beginTransaction().replace(R.id.content_frame, mCurrFragment).commit();

    // update selected item and title
    mDrawerList.setItemChecked(position, true);
    setTitle(mFragmentTitles[position]);
}

From source file:org.proninyaroslav.libretorrent.fragments.MainFragment.java

private void showDetailTorrent(String id) {
    if (Utils.isTwoPane(activity.getApplicationContext())) {
        FragmentManager fm = getFragmentManager();

        DetailTorrentFragment detail = DetailTorrentFragment.newInstance(id);

        Fragment fragment = fm.findFragmentById(R.id.detail_torrent_fragmentContainer);
        if (fragment != null && fragment instanceof DetailTorrentFragment) {
            String oldId = ((DetailTorrentFragment) fragment).getTorrentId();

            if (oldId != null && id.equals(oldId)) {
                return;
            }//  w w  w  . ja v a  2s.  c om
        }

        fm.beginTransaction().replace(R.id.detail_torrent_fragmentContainer, detail)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE).commit();

    } else {
        Intent i = new Intent(activity, DetailTorrentActivity.class);
        i.putExtra(DetailTorrentActivity.TAG_TORRENT_ID, id);
        startActivity(i);
    }
}

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

/**
 * This method controls both fragments, instructing them to display a
 * certain note./*from ww  w  .jav a 2  s.  c o  m*/
 * 
 * @param noteUri The {@link Uri} of the note to show. To create a new note,
 *            pass {@code null}.
 */
private void showNote(final Uri noteUri) {

    if (mTwoPaneView) {
        // check if the NoteEditFragment has been added
        FragmentManager fm = getFragmentManager();
        NoteEditFragment edit = (NoteEditFragment) fm.findFragmentByTag(NOTE_EDIT_TAG);
        final boolean editNoteAdded = (edit != null);

        if (editNoteAdded) {
            if (edit.mCurrentNote != null && edit.mCurrentNote.equals(noteUri)) {
                // clicked on the currently selected note
                return;
            }

            NoteEditFragment editFrag = (NoteEditFragment) fm.findFragmentByTag(NOTE_EDIT_TAG);
            if (noteUri != null) {
                // load an existing note
                editFrag.loadNote(noteUri);
                NoteListFragment list = (NoteListFragment) fm.findFragmentById(R.id.list);
                list.setActivatedNote(Long.valueOf(noteUri.getLastPathSegment()));
            } else {
                // creating a new note - clear the form & list
                // activation
                if (editNoteAdded) {
                    editFrag.clear();
                }
                NoteListFragment list = (NoteListFragment) fm.findFragmentById(R.id.list);
                list.clearActivation();
            }
        } else {
            // add the NoteEditFragment to the container
            FragmentTransaction ft = fm.beginTransaction();
            edit = new NoteEditFragment();
            ft.add(R.id.note_detail_container, edit, NOTE_EDIT_TAG);
            ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
            ft.commit();
            edit.loadNote(noteUri);
        }
    } else {
        startActivity(new Intent(NoteEditFragment.ACTION_VIEW_NOTE, noteUri));
    }
}

From source file:com.android.contacts.activities.PeopleActivity.java

private void createViewsAndFragments(Bundle savedState) {
    // Disable the ActionBar so that we can use a Toolbar. This needs to be called before
    // setContentView().
    getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.people_activity);

    final FragmentManager fragmentManager = getFragmentManager();

    // Hide all tabs (the current tab will later be reshown once a tab is selected)
    final FragmentTransaction transaction = fragmentManager.beginTransaction();

    mTabTitles = new String[TabState.COUNT];
    mTabTitles[TabState.FAVORITES] = getString(R.string.favorites_tab_label);
    mTabTitles[TabState.ALL] = getString(R.string.all_contacts_tab_label);
    mTabPager = getView(R.id.tab_pager);
    mTabPagerAdapter = new TabPagerAdapter();
    mTabPager.setAdapter(mTabPagerAdapter);
    mTabPager.setOnPageChangeListener(mTabPagerListener);

    // Configure toolbar and toolbar tabs. If in landscape mode, we  configure tabs differntly.
    final Toolbar toolbar = getView(R.id.toolbar);
    setActionBar(toolbar);// ww w  . ja  v  a  2s .c o  m
    final ViewPagerTabs portraitViewPagerTabs = (ViewPagerTabs) findViewById(R.id.lists_pager_header);
    ViewPagerTabs landscapeViewPagerTabs = null;
    if (portraitViewPagerTabs == null) {
        landscapeViewPagerTabs = (ViewPagerTabs) getLayoutInflater()
                .inflate(R.layout.people_activity_tabs_lands, toolbar, /* attachToRoot = */ false);
        mViewPagerTabs = landscapeViewPagerTabs;
    } else {
        mViewPagerTabs = portraitViewPagerTabs;
    }
    mViewPagerTabs.setViewPager(mTabPager);

    final String FAVORITE_TAG = "tab-pager-favorite";
    final String ALL_TAG = "tab-pager-all";

    // Create the fragments and add as children of the view pager.
    // The pager adapter will only change the visibility; it'll never create/destroy
    // fragments.
    // However, if it's after screen rotation, the fragments have been re-created by
    // the fragment manager, so first see if there're already the target fragments
    // existing.
    mFavoritesFragment = (ContactTileListFragment) fragmentManager.findFragmentByTag(FAVORITE_TAG);
    mAllFragment = (MultiSelectContactsListFragment) fragmentManager.findFragmentByTag(ALL_TAG);

    if (mFavoritesFragment == null) {
        mFavoritesFragment = new ContactTileListFragment();
        mAllFragment = new MultiSelectContactsListFragment();

        transaction.add(R.id.tab_pager, mFavoritesFragment, FAVORITE_TAG);
        transaction.add(R.id.tab_pager, mAllFragment, ALL_TAG);
    }

    mFavoritesFragment.setListener(mFavoritesFragmentListener);

    mAllFragment.setOnContactListActionListener(new ContactBrowserActionListener());
    mAllFragment.setCheckBoxListListener(new CheckBoxListListener());

    // Hide all fragments for now.  We adjust visibility when we get onSelectedTabChanged()
    // from ActionBarAdapter.
    transaction.hide(mFavoritesFragment);
    transaction.hide(mAllFragment);

    transaction.commitAllowingStateLoss();
    fragmentManager.executePendingTransactions();

    // Setting Properties after fragment is created
    mFavoritesFragment.setDisplayType(DisplayType.STREQUENT);

    mActionBarAdapter = new ActionBarAdapter(this, this, getActionBar(), portraitViewPagerTabs,
            landscapeViewPagerTabs, toolbar);
    mActionBarAdapter.initialize(savedState, mRequest);

    // Add shadow under toolbar
    ViewUtil.addRectangularOutlineProvider(findViewById(R.id.toolbar_parent), getResources());

    // Configure floating action button
    mFloatingActionButtonContainer = findViewById(R.id.floating_action_button_container);
    final ImageButton floatingActionButton = (ImageButton) findViewById(R.id.floating_action_button);
    floatingActionButton.setOnClickListener(this);
    mFloatingActionButtonController = new FloatingActionButtonController(this, mFloatingActionButtonContainer,
            floatingActionButton);
    initializeFabVisibility();

    invalidateOptionsMenuIfNeeded();
}

From source file:com.QuarkLabs.BTCeClient.MainActivity.java

/**
 * Displays selected fragment/*ww w .j  a  va2  s. c  o  m*/
 *
 * @param position Position at the list (0-based)
 */
private void displayItem(final int position) {
    Fragment fragment = null;
    final FragmentManager fragmentManager = getFragmentManager();
    switch (position) {
    case 0:
        fragment = mHomeFragment;
        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        break;
    case 1:
        fragment = new OrdersBookFragment();
        break;
    case 2:
        fragment = new ActiveOrdersFragment();
        break;
    case 3:
        fragment = HistoryFragment.newInstance(ListType.Trades);
        break;
    case 4:
        fragment = HistoryFragment.newInstance(ListType.Transactions);
        break;
    case 5:
        fragment = new ChartsFragment();
        break;
    case 6:
        fragment = new SettingsFragment();
        break;
    case 7:
        fragment = new NotifiersFragment();
        break;
    case 8:
        fragment = new HelpFragment();
        break;
    default:
        break;
    }
    final Fragment fr = fragment;
    if (fr != null) {
        //delay in msecs
        int delay = 250;
        //post delayed for smooth behaviour
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {

                FragmentTransaction transaction = fragmentManager.beginTransaction()
                        .setCustomAnimations(R.animator.fade_in, R.animator.fade_out)
                        .replace(R.id.content_frame, fr);
                if (position != 0) {
                    transaction.addToBackStack(String.valueOf(position)); //name of fragment = position
                }
                transaction.commit();
                setTitle(mDrawerListItems[position]);
            }
        }, delay);
        mDrawerList.setItemChecked(position, true);
        mDrawerList.setSelection(position);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
    }
}

From source file:com.example.search.car.pools.welcome.java

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    Fragment fragment = null;//from  w  w  w .j av  a2s  . c om
    if ((v.equals(l_1) || (v.equals(sp_city)))) {
        String[] city = { "Delhi/NCR", "Bengaluru", "Kolkata", "Mumbai", "Pune", "Ahmedabad" };
        dialog("City", city, sp_city);
    } else if ((v.equals(l_2) || (v.equals(sp_category)))) {
        String[] category = { "All", "Carpool", "Cab", "Rideshare" };
        dialog("Category", category, sp_category);
    } else if ((v.equals(l_3) || (v.equals(sp_search_for)))) {
        String[] search_for = { "Seeker", "Provider", "Both" };
        dialog(" Search For", search_for, sp_search_for);
    } else if (v.equals(close)) {
        promptsView.dismiss();
    } else if (v.equals(rlCities)) {
        layout = "Cities";
        fragment = new Cities();
        frag_tag = "Cities";
        set_fragment(fragment);
        mDrawerLayout.closeDrawer(mDrawerList);
        // getActionBar().setTitle("Cities");
        svg_cities = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.city1);
        iv_cities.setImageDrawable(svg_cities.createPictureDrawable());
        rlCities.setBackgroundColor(Color.parseColor("#00ca98"));
        l_cities.setBackground(getResources().getDrawable(R.drawable.white_circle_side_menu));

        svg_dashboard = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.dashboard);
        iv_dashboard.setImageDrawable(svg_dashboard.createPictureDrawable());
        rlDashboard.setBackgroundColor(Color.parseColor("#2C3E50"));
        l_dashboard.setBackground(getResources().getDrawable(R.drawable.search_blue));
        svg_search = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.search);
        iv_search.setImageDrawable(svg_search.createPictureDrawable());
        rlSearch.setBackgroundColor(Color.parseColor("#2C3E50"));
        l_search.setBackground(getResources().getDrawable(R.drawable.search_blue));
    } else if (v.equals(rlDashboard)) {
        if (task.getString("user_id", null) != null) {
            //            layout = "Dashboard";
            //            fragment = new dashboard();
            //            frag_tag = "Dashboard";
            //            // getActionBar().setTitle("My Profile");
            //            set_fragment(fragment);
            //            mDrawerLayout.closeDrawer(mDrawerList);
            //            svg_dashboard = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.dashboard1);
            //            iv_dashboard.setImageDrawable(svg_dashboard.createPictureDrawable());
            //            rlDashboard.setBackgroundColor(Color.parseColor("#00ca98"));
            //            l_dashboard.setBackground(getResources().getDrawable(R.drawable.white_circle_side_menu));
            //
            //            svg_cities = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.city);
            //            iv_cities.setImageDrawable(svg_cities.createPictureDrawable());
            //            rlCities.setBackgroundColor(Color.parseColor("#2C3E50"));
            //            l_cities.setBackground(getResources().getDrawable(R.drawable.search_blue));
            //            svg_search = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.search);
            //            iv_search.setImageDrawable(svg_search.createPictureDrawable());
            //            rlSearch.setBackgroundColor(Color.parseColor("#2C3E50"));
            //            l_search.setBackground(getResources().getDrawable(R.drawable.search_blue));
            Intent i = new Intent(welcome.this, dashboard_main.class);
            i.putExtra("edit", "1");
            startActivity(i);
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            Toast.makeText(welcome.this, "Please Login First", Toast.LENGTH_SHORT).show();
            mDrawerLayout.closeDrawer(mDrawerList);
        }
    } else if (v.equals(rlSearch)) {
        layout = "Search";
        frag_tag = "Search";
        fragment = new Search();
        set_fragment(fragment);
        mDrawerLayout.closeDrawer(mDrawerList);
        // getActionBar().setTitle("Search");

        svg_search = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.search1);
        iv_search.setImageDrawable(svg_search.createPictureDrawable());
        rlSearch.setBackgroundColor(Color.parseColor("#00ca98"));
        l_search.setBackground(getResources().getDrawable(R.drawable.white_circle_side_menu));

        svg_dashboard = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.dashboard);
        iv_dashboard.setImageDrawable(svg_dashboard.createPictureDrawable());
        rlDashboard.setBackgroundColor(Color.parseColor("#2C3E50"));
        l_dashboard.setBackground(getResources().getDrawable(R.drawable.search_blue));
        svg_cities = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.city);
        iv_cities.setImageDrawable(svg_cities.createPictureDrawable());
        rlCities.setBackgroundColor(Color.parseColor("#2C3E50"));
        l_cities.setBackground(getResources().getDrawable(R.drawable.search_blue));
    } else if (v.equals(rlProfile_login) || v.equals(profile_login)) {
        if (task.getString("user_id", null) == null) {
            Intent i = new Intent(getBaseContext(), user_login.class);
            i.putExtra("frag_id", frag_id);
            startActivity(i);
        }
        mDrawerLayout.closeDrawer(mDrawerList);
    }
    //      else if (v.equals(rlEditProfile)) {
    //         if (task.getString("user_id", null) != null) {
    //            Intent i = new Intent(welcome.this, dashboard_main.class);
    //            i.putExtra("edit", "3");
    //            startActivity(i);
    //         } else {
    //            Toast.makeText(welcome.this, "Please Login First", Toast.LENGTH_SHORT).show();
    //         }
    //         mDrawerLayout.closeDrawer(mDrawerList);
    //      } 
    else if (v.equals(login) || v.equals(rlLogin)) {
        if (task.getString("user_id", null) != null && login.getText().toString().contentEquals("Logout")) {
            SharedPreferences.Editor editor = getSharedPreferences("user", MODE_PRIVATE).edit();
            editor.clear();
            editor.commit();
            login.setText("Login");
            rlDashboard.setVisibility(View.GONE);
            //            rlEditProfile.setVisibility(View.GONE);

            if (layout.contentEquals("Dashboard") || frag_tag.contentEquals("Dashboard")) {
                // highlight search menu on slider during on resume
                // change fragment to search
                FragmentManager fm = getFragmentManager();
                FragmentTransaction fragmentTransaction = fm.beginTransaction();
                fragmentTransaction.replace(R.id.content_frame, new Search());
                fragmentTransaction.commit();
                svg_search = SVGParser.getSVGFromResource(welcome.this.getResources(), R.raw.search1);
                iv_search.setImageDrawable(svg_search.createPictureDrawable());
                rlSearch.setBackgroundColor(Color.parseColor("#00ca98"));
                l_search.setBackground(getResources().getDrawable(R.drawable.white_circle_side_menu));
            }

            iv_login.setImageDrawable(svg_login.createPictureDrawable());
            rlProfile.setVisibility(View.GONE);
            rlProfile_login.setVisibility(View.VISIBLE);
            if (frag_id == 6) {
                Intent i_user = new Intent(getBaseContext(), user_login.class);
                i_user.putExtra("frag_id", frag_id);
                startActivity(i_user);
            }
        } else {
            Intent i = new Intent(getBaseContext(), user_login.class);
            i.putExtra("frag_id", frag_id);
            startActivity(i);
        }
        mDrawerLayout.closeDrawer(mDrawerList);
    } else if (v.equals(b_search)) {
        if (sp_city.getText().toString().toUpperCase().equals("SELECT CITY")) {
            Toast.makeText(welcome.this, "First Select the City", Toast.LENGTH_LONG).show();
        } else {
            Intent i = new Intent(welcome.this, search_result.class);
            i.putExtra("city", sp_city.getText().toString());
            i.putExtra("category", sp_category.getText().toString());
            i.putExtra("search_for", sp_search_for.getText().toString());
            i.putExtra("from", et_from.getText().toString());
            i.putExtra("to", et_to.getText().toString());
            i.putExtra("frag_id", 1);
            i.putExtra("company_id", 0);

            startActivity(i);
            promptsView.dismiss();
        }
    } else if (v.equals(l_nav_search) || v.equals(ib_search)) {
        final int DELAY = 200;
        // ColorDrawable f = new
        // ColorDrawable(Color.parseColor("#0087ca"));
        // ColorDrawable f1 = new
        // ColorDrawable(Color.parseColor("#3398ca"));
        AnimationDrawable a = new AnimationDrawable();
        a.addFrame(d1, DELAY);
        a.addFrame(d2, DELAY);
        a.setOneShot(true);
        l_nav_search.setBackground(a);
        a.start();
        showSearchDialog();
    } else if (v.equals(l_handle) || v.equals(ib_handle)) {
        final int DELAY = 200;
        // ColorDrawable f = new
        // ColorDrawable(Color.parseColor("#0087ca"));
        // ColorDrawable f1 = new
        // ColorDrawable(Color.parseColor("#3398ca"));

        AnimationDrawable a = new AnimationDrawable();
        a.addFrame(d1, DELAY);
        a.addFrame(d2, DELAY);
        a.setOneShot(true);
        l_handle.setBackground(a);
        a.start();
        if (mDrawerLayout.isDrawerOpen(GravityCompat.START)) {
            mDrawerLayout.closeDrawer(mDrawerList);
        } else {
            mDrawerLayout.openDrawer(mDrawerList);
        }
    } else if (v.equals(l_menu) || v.equals(ib_menu)) {
        final int DELAY = 200;
        // for light background
        // ColorDrawable f = new
        // ColorDrawable(Color.parseColor("#0087ca"));
        // ColorDrawable f1 = new
        // ColorDrawable(Color.parseColor("#3398ca"));
        AnimationDrawable a = new AnimationDrawable();
        a.addFrame(d1, DELAY);
        a.addFrame(d2, DELAY);
        a.setOneShot(true);
        l_menu.setBackground(a);
        a.start();

        final PopupMenu popup = new PopupMenu(welcome.this, v);
        popup.getMenuInflater().inflate(R.menu.main, popup.getMenu());

        bedMenuItem = popup.getMenu().findItem(R.id.menu_login);
        final SharedPreferences task = getSharedPreferences("user", MODE_PRIVATE);
        popup.getMenu().findItem(R.id.menu_add_new_list).setVisible(!(task.getString("user_id", null) == null));
        popup.getMenu().findItem(R.id.menu_dashboard).setVisible(!(task.getString("user_id", null) == null));

        if (task.getString("user_id", null) != null) {
            bedMenuItem.setTitle("Logout");
            login.setText("Logout");
            iv_login.setImageDrawable(svg_logout.createPictureDrawable());
            rlProfile.setVisibility(View.VISIBLE);
            rlProfile_login.setVisibility(View.GONE);
            rlDashboard.setVisibility(View.VISIBLE);
            //            rlEditProfile.setVisibility(View.VISIBLE);
            set_data();
        } else {
            bedMenuItem.setTitle("Login/Register");
            login.setText("Login");
            iv_login.setImageDrawable(svg_login.createPictureDrawable());
            rlDashboard.setVisibility(View.GONE);
            //            rlEditProfile.setVisibility(View.GONE);
            rlProfile.setVisibility(View.GONE);
            rlProfile_login.setVisibility(View.VISIBLE);
        }

        if (task.getString("user_id", null) != null) {
            popup.getMenu().findItem(R.id.menu_login).setTitle("Logout");
        } else {
            popup.getMenu().findItem(R.id.menu_login).setTitle("Login/Register");
        }
        popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                // TODO Auto-generated method stub
                int id = item.getItemId();
                Intent i;
                switch (id) {
                case R.id.menu_add_new_list:
                    if (task.getString("user_id", null) != null) {
                        i = new Intent(welcome.this, create_activity.class);
                        startActivity(i);
                    } else {
                        Toast.makeText(welcome.this, "Login first", Toast.LENGTH_SHORT).show();
                    }

                    return true;
                case R.id.menu_dashboard:
                    if (task.getString("user_id", null) != null) {
                        i = new Intent(welcome.this, dashboard_main.class);
                        i.putExtra("edit", "12344");
                        startActivity(i);
                    } else {
                        Toast.makeText(welcome.this, "Please Login first", Toast.LENGTH_LONG).show();
                    }
                    return true;
                case R.id.menu_login:
                    if (bedMenuItem.getTitle().equals("Logout")) {
                        SharedPreferences.Editor editor = getSharedPreferences("user", MODE_PRIVATE).edit();
                        editor.clear();
                        editor.commit();
                        // bedMenuItem.setTitle("Login/Register");
                        login.setText("Login");
                        rlDashboard.setVisibility(View.GONE);
                        //                     rlEditProfile.setVisibility(View.GONE);

                        if (layout.contentEquals("Dashboard") || frag_tag.contentEquals("Dashboard")) {
                            // highlight search menu on slider during on
                            // resume
                            // change fragment to search
                            FragmentManager fm = getFragmentManager();
                            FragmentTransaction fragmentTransaction = fm.beginTransaction();
                            fragmentTransaction.replace(R.id.content_frame, new Search());
                            fragmentTransaction.commit();
                            SVG svg_search = SVGParser.getSVGFromResource(welcome.this.getResources(),
                                    R.raw.search1);
                            iv_search.setImageDrawable(svg_search.createPictureDrawable());
                            rlSearch.setBackgroundColor(Color.parseColor("#00ca98"));
                            l_search.setBackground(
                                    getResources().getDrawable(R.drawable.white_circle_side_menu));
                        }

                        iv_login.setImageDrawable(svg_login.createPictureDrawable());
                        rlProfile.setVisibility(View.GONE);
                        rlProfile_login.setVisibility(View.VISIBLE);
                        if (frag_id == 6) {
                            Intent i_user = new Intent(getBaseContext(), user_login.class);
                            i_user.putExtra("frag_id", frag_id);
                            startActivity(i_user);
                        }
                    } else {
                        i = new Intent(getBaseContext(), user_login.class);
                        i.putExtra("frag_id", frag_id);
                        startActivity(i);
                    }
                    return true;
                }
                return false;
            }
        });
        popup.show();
        // } else {
        // openOptionsMenu();
        // }
    }

}

From source file:com.example.android.indoornavigation.activities.MainActivity.java

private void selectItem(int position) {

    Fragment fragment = null;//from  w  w  w .j a  v  a 2  s. c o  m
    FragmentManager fragmentManager;

    if (position == 3) {
        // CursorLoaderListFragment cursorLoaderListFragment = new CursorLoaderListFragment();
        // fragmentManager = getFragmentManager();
        // fragmentManager.beginTransaction().replace(android.R.id.content, cursorLoaderListFragment).commit();
        startSearchActivity();
    } else if (position == 4) {
        startNavigationActivity();
    } else {
        if (position == 0) {
            fragment = new OSMDroidMapFragment();
            Bundle args = new Bundle();
            args.putInt("status", 0);
            fragment.setArguments(args);
        } else if (position == 1) {
            fragment = new OSMDroidMapFragment();
            Bundle args = new Bundle();
            args.putInt("status", 1);
            fragment.setArguments(args);
        } else if (position == 2) {
            fragment = new OSMDroidMapFragment();
            Bundle args = new Bundle();
            args.putInt("status", 2);
            fragment.setArguments(args);
        }

        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(mNavigationDrawerTitles[position]);
        mDrawerLayout.closeDrawer(mDrawerList);
    }
}

From source file:systems.byteswap.publicstream.MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    store = new StorageProvider(this);

    // find the retained fragment on activity restarts
    FragmentManager fm = getFragmentManager();
    dataFragment = (MainFragment) fm.findFragmentByTag("data");
    // create the fragment and data the first time
    // or load existing data...
    if (dataFragment == null) {
        //create everything necessary new
        createWithoutFragment();//from ww  w  .  java2 s.  co m
        //commit our fragment (containing data) to the FragmentManager
        fm.beginTransaction().add(dataFragment, "data").commit();
    } else {
        //Existing fragment -> restore everything from there
        restoreFromFragment();
    }

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    IntentFilter filter = new IntentFilter();
    filter.setPriority(Integer.MAX_VALUE);
    filter.addAction(MediaService.ACTION_PLAY_PAUSE);
    registerReceiver(mReceiver, filter);
    registerV21();

    /**
    Set up the ViewPager with the sections adapter.
    The {@link ViewPager} that will host the section contents.
    */
    ViewPager mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setPageTransformer(true, new CubeOutTransformer());

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    addGUIListener();
}