Example usage for android.support.v4.app FragmentManager beginTransaction

List of usage examples for android.support.v4.app FragmentManager beginTransaction

Introduction

In this page you can find the example usage for android.support.v4.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:butter.droid.activities.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(NavigationDrawerFragment.NavDrawerItem item, String title) {
    setTitle(null != title ? title : getString(R.string.app_name));
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getSupportFragmentManager();

    String tag = title + "_tag";
    // Fragment fragment = mFragmentCache.get(position);
    mCurrentFragment = fragmentManager.findFragmentByTag(tag);
    if (null == mCurrentFragment && item.hasProvider()) {
        mCurrentFragment = MediaContainerFragment.newInstance(item.getMediaProvider());
    }/*from  w  w w.  j a  v a  2 s .co  m*/

    if (mTabs.getTabCount() > 0)
        mTabs.getTabAt(0).select();

    fragmentManager.beginTransaction().replace(R.id.container, mCurrentFragment, tag).commit();

    if (mCurrentFragment instanceof MediaContainerFragment) {
        updateTabs((MediaContainerFragment) mCurrentFragment,
                ((MediaContainerFragment) mCurrentFragment).getCurrentSelection());
    }
}

From source file:cc.softwarefactory.lokki.android.activities.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int position) {
    String[] menuOptions = getResources().getStringArray(R.array.menuOptions);
    FragmentManager fragmentManager = getSupportFragmentManager();
    mTitle = menuOptions[position];//from  w ww . j  a  va2  s . c  o  m
    selectedOption = position;

    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        actionBar.setTitle(mTitle);
    }
    switch (position) {

    case 0: // Map
        fragmentManager.beginTransaction().replace(R.id.container, new MapViewFragment()).commit();
        break;

    case 1: // Places
        fragmentManager.beginTransaction().replace(R.id.container, new PlacesFragment()).commit();
        break;

    case 2: // People
        fragmentManager.beginTransaction().replace(R.id.container, new ContactsFragment()).commit();
        break;

    case 3: // Settings
        fragmentManager.beginTransaction().replace(R.id.container, new PreferencesFragment()).commit();
        break;

    case 4: // About
        fragmentManager.beginTransaction().replace(R.id.container, new AboutFragment()).commit();
        break;

    default:
        fragmentManager.beginTransaction().replace(R.id.container, new MapViewFragment()).commit();
        break;
    }
    supportInvalidateOptionsMenu();

}

From source file:ca.appvelopers.mcgillmobile.ui.MapActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
    ButterKnife.bind(this);
    App.component(this).inject(this);
    analytics.sendScreen("Map");

    //Set up the initial information
    places = new ArrayList<>();
    shownPlaces = new ArrayList<>();
    searchString = "";
    type = new PlaceType(false);

    FormGenerator fg = FormGenerator.bind(this, container);

    // Icon coloring
    int red = ContextCompat.getColor(this, R.color.red);
    Utils.setTint(directions, 0, red);/*from  w  w  w  .  j  a  v  a2 s. c o  m*/
    Utils.setTint(favorite, 0, red);

    //Set up the place filter
    fg.text(type.getString(this, languagePref.get())).leftIcon(R.drawable.ic_location)
            .rightIcon(R.drawable.ic_chevron_right, Color.GRAY).onClick(new TextViewFormItem.OnClickListener() {
                @Override
                public void onClick(final TextViewFormItem item) {
                    DialogUtils.list(MapActivity.this, R.string.map_filter,
                            new PlaceTypeListAdapter(MapActivity.this, type) {
                                @Override
                                public void onPlaceTypeSelected(PlaceType type) {
                                    MapActivity.this.type = type;

                                    //Update the text
                                    item.view().setText(
                                            type.getString(MapActivity.this, languagePreference.get()));

                                    //Update the filtered places
                                    filterByCategory();
                                }
                            });
                }
            }).build();

    FragmentManager manager = getSupportFragmentManager();
    //Get the MapFragment
    SupportMapFragment fragment = (SupportMapFragment) manager.findFragmentById(R.id.map);
    //If it's null, initialize it and put it in its view
    if (fragment == null) {
        fragment = SupportMapFragment.newInstance();
        manager.beginTransaction().replace(R.id.map, fragment).addToBackStack(null).commit();
    }

    fragment.getMapAsync(this);
}

From source file:com.dazone.crewchat.libGallery.activity.BucketHomeFragmentActivity.java

@SuppressLint("ResourceAsColor")
@Override/*  ww w .j a v  a2  s.c  om*/
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Instance = this;
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_home_media_chooser);

    headerBarTitle = (TextView) findViewById(R.id.titleTextViewFromMediaChooserHeaderBar);
    headerBarCamera = (ImageView) findViewById(R.id.cameraImageViewFromMediaChooserHeaderBar);
    headerBarBack = (ImageView) findViewById(R.id.backArrowImageViewFromMediaChooserHeaderView);
    headerBarDone = (TextView) findViewById(R.id.doneTextViewViewFromMediaChooserHeaderView);
    mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);

    headerBarTitle.setText(getResources().getString(R.string.app_name));
    headerBarCamera.setBackgroundResource(R.drawable.ic_camera_unselect_from_media_chooser_header_bar);
    headerBarCamera.setTag(getResources().getString(R.string.image));

    headerBarBack.setOnClickListener(clickListener);
    headerBarCamera.setOnClickListener(clickListener);
    headerBarDone.setOnClickListener(clickListener);

    if (!MediaChooserConstants.showCameraVideo) {
        headerBarCamera.setVisibility(View.GONE);
    }

    mTabHost.setup(this, getSupportFragmentManager(), R.id.realTabcontent);

    if (MediaChooserConstants.showVideo) {
        mTabHost.addTab(
                mTabHost.newTabSpec("tab2")
                        .setIndicator(getResources().getString(R.string.videos_tab) + "      "),
                BucketVideoFragment.class, null);
    }

    if (MediaChooserConstants.showImage) {
        mTabHost.addTab(
                mTabHost.newTabSpec("tab1")
                        .setIndicator(getResources().getString(R.string.images_tab) + "      "),
                BucketImageFragment.class, null);
    }

    mTabHost.getTabWidget().setBackgroundColor(getResources().getColor(R.color.tabs_color));
    mTabHost.getTabWidget().getChildAt(0).setVisibility(View.GONE);

    for (int i = 0; i < mTabHost.getTabWidget().getChildCount(); i++) {

        View childView = mTabHost.getTabWidget().getChildAt(i);
        TextView textView = (TextView) childView.findViewById(android.R.id.title);

        if (textView.getLayoutParams() instanceof RelativeLayout.LayoutParams) {

            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) textView.getLayoutParams();
            params.addRule(RelativeLayout.CENTER_HORIZONTAL);
            params.addRule(RelativeLayout.CENTER_VERTICAL);
            params.height = RelativeLayout.LayoutParams.MATCH_PARENT;
            params.width = RelativeLayout.LayoutParams.WRAP_CONTENT;
            textView.setLayoutParams(params);

        } else if (textView.getLayoutParams() instanceof LinearLayout.LayoutParams) {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) textView.getLayoutParams();
            params.gravity = Gravity.CENTER;
            textView.setLayoutParams(params);
        }
        textView.setTextColor(ContextCompat.getColor(this, R.color.tabs_title_color));
        textView.setTextSize(convertDipToPixels(10));
    }

    /*      ((TextView)(mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title))).setTextColor(getResources().getColor(R.color.headerbar_selected_tab_color));
          ((TextView)(mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title))).setTextColor(Color.WHITE);*/

    mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {

            FragmentManager fragmentManager = getSupportFragmentManager();
            BucketImageFragment imageFragment = (BucketImageFragment) fragmentManager.findFragmentByTag("tab1");
            BucketVideoFragment videoFragment = (BucketVideoFragment) fragmentManager.findFragmentByTag("tab2");
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            if (tabId.equalsIgnoreCase("tab1")) {

                headerBarTitle.setText(getResources().getString(R.string.image));
                headerBarCamera.setBackgroundResource(R.drawable.selector_camera_button);
                headerBarCamera.setTag(getResources().getString(R.string.image));

                if (imageFragment == null) {
                    BucketImageFragment newImageFragment = new BucketImageFragment();
                    fragmentTransaction.add(R.id.realTabcontent, newImageFragment, "tab1");

                } else {

                    if (videoFragment != null) {
                        fragmentTransaction.hide(videoFragment);
                    }

                    fragmentTransaction.show(imageFragment);

                }
                ((TextView) (mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title)))
                        .setTextColor(getResources().getColor(R.color.headerbar_selected_tab_color));
                ((TextView) (mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title)))
                        .setTextColor(Color.WHITE);

            } else {
                headerBarTitle.setText(getResources().getString(R.string.video));
                headerBarCamera.setBackgroundResource(R.drawable.selector_video_button);
                headerBarCamera.setTag(getResources().getString(R.string.video));

                if (videoFragment == null) {

                    final BucketVideoFragment newVideoFragment = new BucketVideoFragment();
                    fragmentTransaction.add(R.id.realTabcontent, newVideoFragment, "tab2");

                } else {

                    if (imageFragment != null) {
                        fragmentTransaction.hide(imageFragment);
                    }

                    fragmentTransaction.show(videoFragment);
                }

                ((TextView) (mTabHost.getTabWidget().getChildAt(0).findViewById(android.R.id.title)))
                        .setTextColor(Color.WHITE);
                ((TextView) (mTabHost.getTabWidget().getChildAt(1).findViewById(android.R.id.title)))
                        .setTextColor(getResources().getColor(R.color.headerbar_selected_tab_color));

            }

            fragmentTransaction.commit();
        }
    });

    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) headerBarCamera.getLayoutParams();
    params.height = convertDipToPixels(40);
    params.width = convertDipToPixels(40);
    headerBarCamera.setLayoutParams(params);
    headerBarCamera.setScaleType(ScaleType.CENTER_INSIDE);
    headerBarCamera.setPadding(convertDipToPixels(15), convertDipToPixels(15), convertDipToPixels(15),
            convertDipToPixels(15));

}

From source file:cat.wuyingren.rorhelper.activities.MainActivity.java

/** Swaps fragments in the menu_main_complete content view */
private void selectItem(int position) {
    Fragment fragment = new GameListFragment();
    Bundle args = new Bundle();
    switch (position) {
    case 0:/*from w ww  .ja v a2s .  c om*/
    case 3:
    case 8:
        //Headers
        break;
    case 1:
        mTitle = getString(R.string.title_section1);
        fragment.setArguments(args);
        break;
    case 2:
        mTitle = getString(R.string.title_section2);
        fragment = new GameStatusFragment();
        args = new Bundle();
        args.putLong(GameStatusFragment.ARG_GAME_ID, currentGameId);
        fragment.setArguments(args);
        break;
    case 4:
        mTitle = getString(R.string.faction_imperials);
        fragment = prepareFactionFragment(dataSource.getFactionId(currentGameId, mTitle));
        break;
    case 5:
        mTitle = getString(R.string.faction_plutocrats);
        fragment = prepareFactionFragment(dataSource.getFactionId(currentGameId, mTitle));
        break;
    case 6:
        mTitle = getString(R.string.faction_conservatives);
        fragment = prepareFactionFragment(dataSource.getFactionId(currentGameId, mTitle));
        break;
    case 7:
        mTitle = getString(R.string.faction_populists);
        fragment = prepareFactionFragment(dataSource.getFactionId(currentGameId, mTitle));
        break;

    case 9:
        mTitle = getString(R.string.title_section3);
        fragment = new CardListFragment();
        args = new Bundle();
        args.putInt(CardListFragment.ARG_SCENARIO_ID, currentGame.getScenarioID());
        args.putBoolean(CardListFragment.ARG_STATEMEN_ENABLED, false);
        fragment.setArguments(args);
        break;
    case 10:
        mTitle = getString(R.string.title_section4);
        fragment = new CardListFragment();
        args = new Bundle();
        args.putInt(CardListFragment.ARG_SCENARIO_ID, currentGame.getScenarioID());
        args.putBoolean(CardListFragment.ARG_STATEMEN_ENABLED, true);
        fragment.setArguments(args);
        break;
    default:
        break;
    }

    // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

    // Highlight the selected item, update the title, and close the drawer
    mDrawerList.setItemChecked(position, true);
    //getActionBar().setTitle((mOperationsSystem[position]));
    mDrawerLayout.closeDrawer(mDrawerLayout.findViewById(R.id.drawerRoot));
}

From source file:bf.io.openshop.ux.MainActivity.java

/**
 * Add first fragment to the activity. This fragment will be attached to the bottom of the fragments stack.
 * When fragment stack is cleared {@link #clearBackStack}, this fragment will be shown.
 *///from  w w w  . ja v a  2 s .  c  om
private void addInitialFragment() {
    Fragment fragment = new BannersFragment();
    FragmentManager frgManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = frgManager.beginTransaction();
    fragmentTransaction.add(R.id.main_content_frame, fragment).commit();
    frgManager.executePendingTransactions();
}

From source file:bf.io.openshop.ux.MainActivity.java

/**
 * Method creates fragment transaction and replace current fragment with new one.
 *
 * @param newFragment    new fragment used for replacement.
 * @param transactionTag text identifying fragment transaction.
 *//*  w ww .  j  a v  a2  s .  c om*/
private void replaceFragment(Fragment newFragment, String transactionTag) {
    if (newFragment != null) {
        FragmentManager frgManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = frgManager.beginTransaction();
        fragmentTransaction.addToBackStack(transactionTag);
        fragmentTransaction.replace(R.id.main_content_frame, newFragment).commit();
        frgManager.executePendingTransactions();
    } else {
        Timber.e(new RuntimeException(), "Replace fragments with null newFragment parameter.");
    }
}

From source file:com.android.calendar.agenda.AgendaFragment.java

private void showEventInfo(EventInfo event, boolean allDay, boolean replaceFragment) {

    // Ignore unknown events
    if (event.id == -1) {
        Log.e(TAG, "showEventInfo, event ID = " + event.id);
        return;//from www .  j av  a  2s  . c o m
    }

    mLastShownEventId = event.id;

    // Create a fragment to show the event to the side of the agenda list
    if (mShowEventDetailsWithAgenda) {
        FragmentManager fragmentManager = getFragmentManager();
        if (fragmentManager == null) {
            // Got a goto event before the fragment finished attaching,
            // stash the event and handle it later.
            mOnAttachedInfo = event;
            mOnAttachAllDay = allDay;
            return;
        }
        FragmentTransaction ft = fragmentManager.beginTransaction();

        if (allDay) {
            event.startTime.timezone = Time.TIMEZONE_UTC;
            event.endTime.timezone = Time.TIMEZONE_UTC;
        }

        if (DEBUG) {
            Log.d(TAG, "***");
            Log.d(TAG, "showEventInfo: start: " + new Date(event.startTime.toMillis(true)));
            Log.d(TAG, "showEventInfo: end: " + new Date(event.endTime.toMillis(true)));
            Log.d(TAG, "showEventInfo: all day: " + allDay);
            Log.d(TAG, "***");
        }

        long startMillis = event.startTime.toMillis(true);
        long endMillis = event.endTime.toMillis(true);
        EventInfoFragment fOld = (EventInfoFragment) fragmentManager.findFragmentById(R.id.agenda_event_info);
        if (fOld == null || replaceFragment || fOld.getStartMillis() != startMillis
                || fOld.getEndMillis() != endMillis || fOld.getEventId() != event.id) {
            mEventFragment = new EventInfoFragment(mActivity, event.id, startMillis, endMillis,
                    Attendees.ATTENDEE_STATUS_NONE, false, EventInfoFragment.DIALOG_WINDOW_STYLE, null);
            ft.replace(R.id.agenda_event_info, mEventFragment);
            ft.commit();
        } else {
            fOld.reloadEvents();
        }
    }
}

From source file:com.cerema.cloud2.ui.activity.Uploader.java

/**
 * Show waiting for copy dialog/*from w w w . j  a va  2  s.com*/
 */
public void showWaitingCopyDialog() {
    // Construct dialog
    LoadingDialog loading = new LoadingDialog(
            getResources().getString(R.string.wait_for_tmp_copy_from_private_storage));
    FragmentManager fm = getSupportFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    loading.show(ft, DIALOG_WAIT_COPY_FILE);

}

From source file:com.desno365.mods.Activities.MainActivity.java

@Override
public void onNavigationDrawerItemSelected(int groupPosition, int childPosition) {
    if (groupPosition == 1) {
        if (mAppSectionsPagerAdapter != null) {
            mViewPager.setCurrentItem(childPosition + 1); // + 1 because the home Fragment of the ViewPager isn't in the mods group
        } else {//  w  w  w .j  ava 2 s . c  o m
            // update the main content by replacing fragments
            Fragment myFragment;
            switch (childPosition + 1) {
            case DesnoGuns.viewPagerPosition:
                myFragment = DesnoGuns.getFragmentTab();
                break;
            case Portal.viewPagerPosition:
                myFragment = Portal.getFragmentTab();
                break;
            case Laser.viewPagerPosition:
                myFragment = Laser.getFragmentTab();
                break;
            case Turrets.viewPagerPosition:
                myFragment = Turrets.getFragmentTab();
                break;
            case Jukebox.viewPagerPosition:
                myFragment = Jukebox.getFragmentTab();
                break;
            case Unreal.viewPagerPosition:
                myFragment = Unreal.getFragmentTab();
                break;
            default:
                myFragment = new FragmentTab1();
                break;
            }
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.fragment_container, myFragment).commit();
        }
    }
}