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.zedpine.leaguechat.MainActivity.java

private void selectItem(int position) {

    FragmentManager fragmentManager = getFragmentManager();

    if (position == 1) {
        Fragment settingsFrag = new SettingsFragment();
        Bundle args = new Bundle();
        args.putInt("SettingsItem", position);
        settingsFrag.setArguments(args);
        fragmentManager.beginTransaction().replace(R.id.content_frame, settingsFrag).commit();
    } else if (position == 4) {
        Fragment newpost = new AddNewPostFragment();
        Bundle args = new Bundle();
        args.putInt("SettingsItem", position);
        newpost.setArguments(args);// ww  w .  j a  va  2s . c  o m
        fragmentManager.beginTransaction().replace(R.id.content_frame, newpost).commit();
    } else {
        Fragment settingsFrag = new SettingsFragment();
        Bundle args = new Bundle();
        args.putInt("SettingsItem", position);
        settingsFrag.setArguments(args);
        fragmentManager.beginTransaction().replace(R.id.content_frame, settingsFrag).commit();
    }
    // update selected item and title, then close the drawer
    setTitle(settingsName[position]);
    position = position + 1;
    mDrawerList.setItemChecked(position, true);
    mDrawerLayout.closeDrawer(mDrawerList);
}

From source file:com.zte.permissioncontrol.ui.PermissionControlPageActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.permission_control_pages);
    mEmptyView = (FrameLayout) findViewById(R.id.empty_view);
    mSavedInstanceState = savedInstanceState;
    // add the switch on Action bar
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mSwitch = (Switch) inflater.inflate(R.layout.imageswitch_layout, null);
    final int padding = getResources().getDimensionPixelSize(R.dimen.action_bar_switch_padding);
    mSwitch.setPadding(0, 0, padding, 0);
    getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM, ActionBar.DISPLAY_SHOW_CUSTOM);
    getActionBar().setCustomView(mSwitch, new ActionBar.LayoutParams(ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL | Gravity.END));
    mSwitch.setOnCheckedChangeListener(this);

    // hide fragment firstly , then update it in onResume() according to switch status
    final FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction transaction = fragmentManager.beginTransaction();

    mPermissionsFragment = (PermissionsFragment) fragmentManager.findFragmentByTag(mPermissionsTag);
    mAppsFragment = (AppsFragment) fragmentManager.findFragmentByTag(mAppsTag);

    if (mPermissionsFragment == null) {
        mPermissionsFragment = new PermissionsFragment();
        mAppsFragment = new AppsFragment();
        transaction.add(R.id.tab_pager, mPermissionsFragment, mPermissionsTag);
        transaction.add(R.id.tab_pager, mAppsFragment, mAppsTag);
    }//from   ww w  .  jav  a  2s. co m

    transaction.hide(mPermissionsFragment);
    transaction.hide(mAppsFragment);
    transaction.commit();
    fragmentManager.executePendingTransactions();

    // set page adapter
    mTabPager = (ViewPager) findViewById(R.id.tab_pager);
    mTabPagerAdapter = new TabPagerAdapter();
    mTabPager.setAdapter(mTabPagerAdapter);
    mTabPager.setOnPageChangeListener(mTabPagerListener);

    // Configure action bar
    mActionBarAdapter = new ActionBarAdapter(this, this, getActionBar());

    boolean isShow = PermControlUtils.isPermControlOn(this);
    Log.d(TAG, "oncreate(), isShow " + isShow);

}

From source file:it.gmariotti.cardslib.demo.MainNativeActivity.java

private void openFragment(BaseFragment baseFragment) {
    if (baseFragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.fragment_main, baseFragment);
        fragmentTransaction.addToBackStack(null);

        fragmentTransaction.commit();//  ww  w.  j  a  v a 2 s  . c om
        if (baseFragment.getTitleResourceId() > 0)
            mCurrentTitle = baseFragment.getTitleResourceId();

    }
}

From source file:com.ffmpeger.card.MainNativeActivity.java

private void openFragment(BaseFragment baseFragment) {
    if (baseFragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.replace(com.ffmpeger.card.R.id.fragment_main, baseFragment);
        fragmentTransaction.addToBackStack(null);

        fragmentTransaction.commit();//from www  .java 2  s.  c om
        if (baseFragment.getTitleResourceId() > 0)
            mCurrentTitle = baseFragment.getTitleResourceId();

    }
}

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

private void openFragment(BaseFragment baseFragment) {
    if (baseFragment != null) {
        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.replace(R.id.fragment_main, baseFragment);
        //fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();//from ww  w. j av  a  2s  .  c  om
        if (baseFragment.getTitleResourceId() > 0)
            mCurrentTitle = baseFragment.getTitleResourceId();

    }
}

From source file:com.ideabytes.dgsms.ca.HomeActivity.java

/**
 * This method is to select default selection as home activity from navigation drawer
        /*from  w  ww.j av a  2  s  .c om*/
 */
private void defaultSelection(final Fragment fragment, final int position) {
    try {
        if (fragment != null) {
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().add(R.id.frame_container, fragment).commit();

            // update selected item and title, then close the drawer
            drawerList.setItemChecked(position, true);
            drawerList.setSelection(position);
            setTitle(listItems[position]);
            drawerLayout.closeDrawer(drawerList);
        } else {
            // error in creating fragment
            Log.e("MainActivity", "Error in creating fragment");
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:mx.openpay.android.example.AddCardActivity.java

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

    this.progressFragment = ProgressDialogFragment.newInstance(R.string.progress_message);

    FragmentManager fm = this.getFragmentManager();
    this.deviceIdFragment = (DeviceIdFragment) fm.findFragmentByTag("DeviceCollector");
    // If not retained (or first time running), we need to create it.
    if (this.deviceIdFragment == null) {
        this.deviceIdFragment = new DeviceIdFragment();
        fm.beginTransaction().add(this.deviceIdFragment, "DeviceCollector").commit();
    }//  w  ww  .  j av  a 2s .  co m
    final RadioButton radio1 = (RadioButton) this.findViewById(R.id.radioButton1);
    radio1.setChecked(true);

}

From source file:com.matthewmitchell.peercoin_android_wallet.ui.AddressBookActivity.java

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

    setContentView(R.layout.address_book_content);

    final ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);

    final FragmentManager fragmentManager = getFragmentManager();

    walletAddressesFragment = (WalletAddressesFragment) fragmentManager.findFragmentByTag(TAG_LEFT);
    sendingAddressesFragment = (SendingAddressesFragment) fragmentManager.findFragmentByTag(TAG_RIGHT);

    final FragmentTransaction removal = fragmentManager.beginTransaction();

    if (walletAddressesFragment == null)
        walletAddressesFragment = new WalletAddressesFragment();
    else/* w  ww .  ja v  a  2  s.  c om*/
        removal.remove(walletAddressesFragment);

    if (sendingAddressesFragment == null)
        sendingAddressesFragment = new SendingAddressesFragment();
    else
        removal.remove(sendingAddressesFragment);

    if (!removal.isEmpty()) {
        removal.commit();
        fragmentManager.executePendingTransactions();
    }

    final ViewPager pager = (ViewPager) findViewById(R.id.address_book_pager);
    if (pager != null) {
        pager.setAdapter(
                new TwoFragmentAdapter(fragmentManager, walletAddressesFragment, sendingAddressesFragment));

        final ViewPagerTabs pagerTabs = (ViewPagerTabs) findViewById(R.id.address_book_pager_tabs);
        pagerTabs.addTabLabels(R.string.address_book_list_receiving_title,
                R.string.address_book_list_sending_title);

        pager.setOnPageChangeListener(pagerTabs);
        final int position = 1;
        pager.setCurrentItem(position);
        pager.setPageMargin(2);
        pager.setPageMarginDrawable(R.color.bg_less_bright);

        pagerTabs.onPageSelected(position);
        pagerTabs.onPageScrolled(position, 0, 0);
    } else {
        fragmentManager.beginTransaction()
                .add(R.id.wallet_addresses_fragment, walletAddressesFragment, TAG_LEFT)
                .add(R.id.sending_addresses_fragment, sendingAddressesFragment, TAG_RIGHT).commit();
    }

    runAfterLoad(new Runnable() {

        @Override
        public void run() {
            updateFragments();
        }

    });

}

From source file:de.guj.ems.mobile.sdk.test.MainActivity.java

private void selectItem(int position) {
    // update the main content by replacing fragments
    if ("Retina".equals(mSampleTitles[position])) {
        Fragment fragment = new RetinaBannerTest();
        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(mSampleTitles[position]);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }/*from  ww  w .  j  ava2s .  co  m*/
        ;
    }
    if ("Web ListView".equals(mSampleTitles[position])) {
        Fragment fragment = new ListViewTest();
        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(mSampleTitles[position]);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
    }
    if ("Native ListView".equals(mSampleTitles[position])) {
        Fragment fragment = new NativeListViewTest();
        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(mSampleTitles[position]);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
    }
    if ("Web RelativeLayout".equals(mSampleTitles[position])) {
        Fragment fragment = new RelativeLayoutTest();
        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(mSampleTitles[position]);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
    }
    if ("Native RelativeLayout".equals(mSampleTitles[position])) {
        Fragment fragment = new NativeRelativeLayoutTest();
        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(mSampleTitles[position]);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
    }
    if ("TableLayout".equals(mSampleTitles[position])) {
        Fragment fragment = new TableLayoutTest();
        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(mSampleTitles[position]);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
    }
    if ("Interstitial".equals(mSampleTitles[position])) {
        // get the interstitial receiver's intent
        Intent i = new Intent(MainActivity.this, InterstitialSwitchReceiver.class);
        // configure it, this interstitial has no target,
        // i.e. it returns to the previous activity when finishing
        i.putExtra("ems_zoneId", "15310");
        i.putExtra("ems_uid", Boolean.valueOf(true));
        i.putExtra("ems_geo", Boolean.valueOf(true));
        sendBroadcast(i);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
        setTitle(mSampleTitles[position]);
    }
    if ("VideoInterstitial".equals(mSampleTitles[position])) {
        double rand = Math.random();
        Log.i(TAG, rand >= 0.5 ? "Wrapped Video Interstitial" : "Video Interstitial");
        // get the interstitial receiver's intent
        Intent i = new Intent(MainActivity.this, InterstitialSwitchReceiver.class);
        // configure it, this interstitial has no target,
        // i.e. it returns to the previous activity when finishing
        i.putExtra("ems_zoneId", rand >= 0.5 ? "9002" : "9001");
        i.putExtra("ems_uid", Boolean.valueOf(true));
        i.putExtra("ems_geo", Boolean.valueOf(true));
        // i.putExtra("unmuted", Boolean.valueOf(true));

        sendBroadcast(i);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
        setTitle(mSampleTitles[position]);
    }
    if ("Targeting".equals(mSampleTitles[position])) {
        Fragment fragment = new TargetingTest();
        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(mSampleTitles[position]);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
    }
    if ("ORMMA".equals(mSampleTitles[position])) {
        Fragment fragment = new ORMMATest();
        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(mSampleTitles[position]);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
    }
    if ("PreRoll".equals(mSampleTitles[position])) {
        Log.i(TAG, "G+J EMS PreRolls");
        double rand = Math.random();
        Intent target = new Intent(MainActivity.this, VideoPlayer.class);
        Log.i(TAG, rand >= 0.5 ? "Wrapped Video Interstitial" : "Video Interstitial");
        Intent i = new Intent(MainActivity.this, InterstitialSwitchReceiver.class);
        i.putExtra("ems_zoneId", rand >= 0.5 ? "9002" : "9001");
        i.putExtra("ems_uid", Boolean.valueOf(true));
        i.putExtra("ems_geo", Boolean.valueOf(true));
        i.putExtra("unmuted", Boolean.valueOf(true));
        i.putExtra("target", target);
        sendBroadcast(i);
        setTitle(mSampleTitles[position]);
        if (mDrawerLayout != null) {
            mDrawerLayout.closeDrawer(mDrawerList);
        }
        ;
    }
}

From source file:de.sourcestream.movieDB.controller.TVDetailsInfo.java

/**
 * Callback method to be invoked when an item in this AdapterView has been clicked.
 *
 * @param parent   The AdapterView where the click happened.
 * @param view     The view within the AdapterView that was clicked (this will be a view provided by the adapter)
 * @param position The position of the view in the adapter.
 * @param id       The row id of the item that was clicked.
 *//*w w  w.  j av a  2  s  .  co  m*/
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    activity.setRestoreMovieDetailsAdapterState(true);
    activity.setRestoreMovieDetailsState(false);
    if (activity.getTvDetailsSimFragment() != null
            && activity.getLastVisitedSimTV() == similarList.get(position).getId()
            && activity.getTvDetailsSimFragment().getTimeOut() == 0) {
        // Old tv details retrieve info and re-init component else crash
        activity.getTvDetailsSimFragment().onSaveInstanceState(new Bundle());
        Bundle bundle = new Bundle();
        bundle.putInt("id", similarList.get(position).getId());
        Bundle save = activity.getTvDetailsSimFragment().getSave();
        // Re-init movie details and set save information
        tvDetails = new TVDetails();
        tvDetails.setTimeOut(0);
        tvDetails.setSave(save);
        tvDetails.setArguments(bundle);
    } else
        tvDetails = new TVDetails();

    activity.setLastVisitedSimTV(similarList.get(position).getId());
    activity.getTvDetailsFragment().setAddToBackStack(true);
    activity.getTvDetailsFragment().onSaveInstanceState(new Bundle());
    if (activity.getSearchViewCount())
        activity.incSearchTvDetails();

    activity.setTvDetailsFragment(null);
    activity.setSaveInTVDetailsSimFragment(true);
    tvDetails.setTitle(similarList.get(position).getTitle());
    FragmentManager manager = getActivity().getFragmentManager();
    FragmentTransaction transaction = manager.beginTransaction();
    Bundle bundle = new Bundle();
    bundle.putInt("id", similarList.get(position).getId());
    tvDetails.setArguments(bundle);
    transaction.replace(R.id.frame_container, tvDetails);
    // add the current transaction to the back stack:
    transaction.addToBackStack("similarDetails");
    transaction.commit();

}