Example usage for android.app FragmentManager getFragment

List of usage examples for android.app FragmentManager getFragment

Introduction

In this page you can find the example usage for android.app FragmentManager getFragment.

Prototype

public abstract Fragment getFragment(Bundle bundle, String key);

Source Link

Document

Retrieve the current Fragment instance for a reference previously placed with #putFragment(Bundle,String,Fragment) .

Usage

From source file:com.ape.filemanager.FileExplorerTabActivity.java

private void restoreOrCreateFragment(Bundle savedInstanceState) {
    Fragment tmpFragment = null;// w ww  .j  a  v a  2s. co  m
    Fragment categoryFragment = null;
    Fragment sdCardFragment = null;
    Fragment remoteFragment = null;
    FragmentManager fm = getFragmentManager();

    if (savedInstanceState != null) {
        tmpFragment = fm.getFragment(savedInstanceState, Util.FRAGMENT_CATEGORY);
        if (tmpFragment != null)
            categoryFragment = tmpFragment;

        tmpFragment = fm.getFragment(savedInstanceState, Util.FRAGMENT_SDCARD);
        if (tmpFragment != null)
            sdCardFragment = tmpFragment;

        tmpFragment = fm.getFragment(savedInstanceState, Util.FRAGMENT_REMOTE);
        if (tmpFragment != null)
            remoteFragment = tmpFragment;
    }

    if (categoryFragment == null) {
        categoryFragment = new FileCategoryActivityMyOS();
    }

    if (sdCardFragment == null) {
        sdCardFragment = new FileViewActivity();
    }

    if (remoteFragment == null) {
        remoteFragment = new CloudFileActivity();
    }

    mFragmentAdapter.addFragment(categoryFragment, mTabTitles[Util.CATEGORY_TAB_INDEX],
            mTabIcons[Util.CATEGORY_TAB_INDEX]);
    mFragmentAdapter.addFragment(sdCardFragment, mTabTitles[Util.SDCARD_TAB_INDEX],
            mTabIcons[Util.SDCARD_TAB_INDEX]);
    mFragmentAdapter.addFragment(remoteFragment, mTabTitles[Util.REMOTE_TAB_INDEX],
            mTabIcons[Util.REMOTE_TAB_INDEX]);
}

From source file:us.cboyd.android.dicom.DcmBrowser.java

/** Called when the activity is first created. */
@Override/* ww  w  . j  a  v a2 s .co m*/
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dcm_browser);

    FragmentManager fragManager = getFragmentManager();
    if (savedInstanceState != null) {
        mListFragment = (DcmListFragment) fragManager.getFragment(savedInstanceState, DcmVar.FRAGLIST);
        mInfoFragment = (DcmInfoFragment) fragManager.getFragment(savedInstanceState, DcmVar.FRAGINFO);

        // Remove existing fragments from associated views.
        fragManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        fragManager.beginTransaction().remove(mListFragment).commit();
        fragManager.beginTransaction().remove(mInfoFragment).commit();
        fragManager.executePendingTransactions();
    }

    // Restore the retained fragments, if this is a configuration change.
    if (mListFragment == null) {
        mListFragment = new DcmListFragment();
    }

    if (mInfoFragment == null) {
        mInfoFragment = new DcmInfoFragment();
    }

    // Specify that the Home/Up button should not be enabled,
    // since there is no hierarchical parent yet.
    ActionBar actionBar = getActionBar();
    // enable ActionBar app icon to behave as action to toggle nav drawer
    actionBar.setDisplayHomeAsUpEnabled(false);
    actionBar.setHomeButtonEnabled(false);

    // Check whether the activity is using the layout version with
    // the fragment_container FrameLayout. If so, we must add the first fragment
    if (findViewById(R.id.fragment_container) != null) {
        Log.i("cpb", "mListFrag: One-pane");
        mFragmented = true;

        // Add the fragment to the 'fragment_container' FrameLayout
        fragManager.beginTransaction().add(R.id.fragment_container, mListFragment).commit();

        generateDrawer();
    } else {
        Log.i("cpb", "mListFrag: Two-pane");
        mFragmented = false;

        // Add the fragments to the respective FrameLayouts
        fragManager.beginTransaction().add(R.id.fragment_left, mListFragment).commit();
        fragManager.beginTransaction().add(R.id.fragment_right, mInfoFragment).commit();
    }
}