Android FragmentManager - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Android FragmentManager class.

This code example shows how to use the following methods:beginTransaction, findFragmentByTag
     * @param tag 
     */ 
    protected static void show(DialogFragmentActivity activity,
            DialogFragmentHelper fragment, Bundle arguments, String tag) {
        FragmentManager manager = activity.getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment current = manager.findFragmentByTag(tag);
        if (current != null)
            transaction.remove(current);
        transaction.addToBackStack(null);
 
        fragment.setArguments(arguments);
        fragment.show(manager, tag);
    } 
 
    /** 
     * Create bundle with standard arguments 
     * 
     * @param title 
     * @param message 
9
CodeRank
This code example shows how to use the following methods:show, beginTransaction, findFragmentByTag
This code example shows how to use the following methods:show, beginTransaction, findFragmentByTag
            return this;
        } 
 
        public void show() { 
            FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 
            Fragment prev = fragmentManager.findFragmentByTag(FRAGMENT_TAG);
            if (prev != null) {
                fragmentTransaction.remove(prev);
            } 
            fragmentTransaction.addToBackStack(null);
 
            ProgressDialogFragment.newInstance(mMessage, mOnCancelListener, mCancelable)
                    .show(fragmentManager, FRAGMENT_TAG);
        } 
    } 
 
    public static void dismiss(FragmentActivity activity) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
7
CodeRank
This code example shows how to use the following methods:show, beginTransaction, findFragmentByTag
            return this;
        } 
 
        public void show() { 
            FragmentManager fragmentManager = mActivity.getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
 
            Fragment prev = fragmentManager.findFragmentByTag(FRAGMENT_TAG);
            if (prev != null) {
                fragmentTransaction.remove(prev);
            } 
            fragmentTransaction.addToBackStack(null);
 
            ErrorDialogFragment.newInstance(mTitle, mMessage, mOnClickListener, mOnCancelListener)
                    .show(fragmentManager, FRAGMENT_TAG);
        } 
    } 
 
    public static void dismiss(FragmentActivity activity) {
        FragmentManager fragmentManager = activity.getSupportFragmentManager();
7
CodeRank
This code example shows how to use the following methods:show, beginTransaction, findFragmentByTag
This code example shows how to use the following methods:beginTransaction, findFragmentByTag
            super.onActivityCreated(savedInstanceState);
 
            FragmentManager fm = getFragmentManager();
 
            // Check to see if we have retained the worker fragment. 
            mWorkFragment = (RetainedFragment)fm.findFragmentByTag("work");
 
            // If not retained (or first time running), we need to create it. 
            if (mWorkFragment == null) {
                mWorkFragment = new RetainedFragment();
                // Tell it who it is working with. 
                mWorkFragment.setTargetFragment(this, 0);
                fm.beginTransaction().add(mWorkFragment, "work").commit();
            } 
        } 
 
    } 
 
    /** 
     * This is the Fragment implementation that will be retained across 
7
CodeRank
This code example shows how to use the following methods:beginTransaction, findFragmentByTag
This code example shows how to use the following methods:beginTransaction, findFragmentByTag
This code example shows how to use the following methods:beginTransaction, findFragmentById
        super.onCreate(savedInstanceState);
 
        FragmentManager fm = getSupportFragmentManager();
 
        // Create the list fragment and add it as our sole content. 
        if (fm.findFragmentById(android.R.id.content) == null) {
            CursorLoaderListFragment list = new CursorLoaderListFragment();
            fm.beginTransaction().add(android.R.id.content, list).commit();
        } 
    } 
 
 
    public static class CursorLoaderListFragment extends ListFragment
            implements LoaderManager.LoaderCallbacks<Cursor> {
 
        // This is the Adapter being used to display the list's data. 
        SimpleCursorAdapter mAdapter;
 
        // If non-null, this is the current filter the user has provided. 
        String mCurFilter;
7
CodeRank
This code example shows how to use the following methods:beginTransaction, executePendingTransactions, findFragmentByTag
	} 
 
	private SendTagsFragment getSendTagsFragment() 
	{ 
		FragmentManager fragmentManager = getSupportFragmentManager();
		SendTagsFragment sendTagsFragment =
				(SendTagsFragment) fragmentManager.findFragmentByTag(SEND_TAGS_STATUS_FRAGMENT_TAG);
 
		if (null == sendTagsFragment)
		{ 
			sendTagsFragment = new SendTagsFragment();
			sendTagsFragment.setRetainInstance(true);
			fragmentManager.beginTransaction().add(sendTagsFragment, SEND_TAGS_STATUS_FRAGMENT_TAG).commit();
			fragmentManager.executePendingTransactions();
		} 
 
		return sendTagsFragment;
	} 
 
	@Override 
6
CodeRank
This code example shows how to use the following methods:beginTransaction, findFragmentByTag
            super.onActivityCreated(savedInstanceState);
 
            FragmentManager fm = getFragmentManager();
 
            // Check to see if we have retained the worker fragment. 
            mWorkFragment = (RetainedFragment)fm.findFragmentByTag("work");
 
            // If not retained (or first time running), we need to create it. 
            if (mWorkFragment == null) {
                mWorkFragment = new RetainedFragment();
                // Tell it who it is working with. 
                mWorkFragment.setTargetFragment(this, 0);
                fm.beginTransaction().add(mWorkFragment, "work").commit();
            } 
        } 
 
    } 
 
    /** 
     * This is the Fragment implementation that will be retained across 
7
CodeRank
This code example shows how to use the following methods:
 
import static android.support.v4.app.Watson.OnCreateOptionsMenuListener; 
import static android.support.v4.app.Watson.OnOptionsItemSelectedListener; 
import static android.support.v4.app.Watson.OnPrepareOptionsMenuListener; 
 
public class SherlockFragment extends Fragment implements OnCreateOptionsMenuListener, OnPrepareOptionsMenuListener, OnOptionsItemSelectedListener {
    private SherlockFragmentActivity mActivity;
 
    public SherlockFragmentActivity getSherlockActivity() { 
        return mActivity;
    } 
 
    @Override 
    public void onAttach(Activity activity) {
        if (!(activity instanceof SherlockFragmentActivity)) {
            throw new IllegalStateException(getClass().getSimpleName() + " must be attached to a SherlockFragmentActivity.");
        } 
        mActivity = (SherlockFragmentActivity)activity;
 
        super.onAttach(activity);
10
CodeRank
This code example shows how to use the following methods:beginTransaction, findFragmentByTag
This code example shows how to use the following methods:beginTransaction, findFragmentById
		super.onCreate(savedInstanceState);
 
		FragmentManager fm = getSupportFragmentManager();
 
		// Create the list fragment and add it as our sole content. 
		if (fm.findFragmentById(android.R.id.content) == null)
		{ 
			ReceiversListFragment list = new ReceiversListFragment();
			fm.beginTransaction().add(android.R.id.content, list).commit();
		} 
	} 
 
	public static class ReceiversListFragment extends SherlockListFragment 
	{ 
 
		/** 
		 * The logging TAG 
		 */ 
		private static final String TAG = "ReceiversListFragment";
 
6
CodeRank
This code example shows how to use the following methods:setRetainInstance
	 
	public ColorFragment() {  
		this(R.color.white);
	} 
	 
	public ColorFragment(int colorRes) {
		mColorRes = colorRes;
		setRetainInstance(true);
	} 
 
	@Override 
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		if (savedInstanceState != null)
			mColorRes = savedInstanceState.getInt("mColorRes");
		int color = getResources().getColor(mColorRes);
		// construct the RelativeLayout 
		RelativeLayout v = new RelativeLayout(getActivity());
		v.setBackgroundColor(color);		
		return v;
	} 
10
CodeRank
This code example shows how to use the following methods:
package com.newsblur.fragment; 
 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
 
import com.newsblur.R; 
 
public class LoadingFragment extends Fragment {
	 
	@Override 
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_loadingitems, null);
	} 
 
} 

10
CodeRank
This code example shows how to use the following methods:getBackStackEntryCount
	} 
 
	@Override 
	public void clearFragments() { 
		FragmentManager fm = getSupportFragmentManager();
		for(int i = 0; i < fm.getBackStackEntryCount(); i ++)    
			fm.popBackStack();
	} 
 
	@Override 
	public void setMenuFragment(Fragment f) {
		FragmentManager fm = getSupportFragmentManager();
		FragmentTransaction ft = fm.beginTransaction();
		ft.replace(R.id.menu_frame, f);
		ft.commit();
 
		wMenuFragment = new WeakReference<Fragment>(f);
	} 
 
	@Override 
6
CodeRank
This code example shows how to use the following methods:beginTransaction
	} 
 
 
	private void setInitialFragment() { 
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
		fragmentTransaction.add(CONTENT_VIEW_ID, MainFragment.newInstance()).commit();
	} 
 
} 
6
CodeRank
This code example shows how to use the following methods:beginTransaction, executePendingTransactions, findFragmentByTag, getBackStackEntryAt, getBackStackEntryCount
  	} 
  } 
   
  private void removePreviousFragment() { 
    FragmentManager fm = getSupportFragmentManager();
    int count = fm.getBackStackEntryCount();
    if (count > 0) {
    	FragmentTransaction ft = fm.beginTransaction();
      ft.setCustomAnimations(android.R.anim.slide_in_left, android.R.anim.slide_out_right, android.R.anim.slide_out_right, android.R.anim.slide_in_left);
    	//ft.hide(mCurrentFragment); 
      fm.popBackStack();
      fm.executePendingTransactions();
      if (count > 1) {
        String name = fm.getBackStackEntryAt(count-2).getName();
        mCurrentFragment = fm.findFragmentByTag(name);
      } else { 
        mCurrentFragment = null;
      } 
    } 
  } 
6
CodeRank
This code example shows how to use the following methods:
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */ 
 
package com.actionbarsherlock.sample.fragments; 
 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.support.v4.app.FragmentTransaction; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.TextView; 
import com.actionbarsherlock.app.SherlockFragment; 
import com.actionbarsherlock.app.SherlockFragmentActivity; 
10
CodeRank
This code example shows how to use the following methods:beginTransaction, findFragmentByTag
 
        // Check to see if we already have a fragment for this tab, probably 
        // from a previously saved state.  If so, deactivate it, because our 
        // initial state is that a tab isn't shown. 
        FragmentManager fm = mActivity.getSupportFragmentManager();
        mFragment = fm.findFragmentByTag(mTag);
        if (mFragment != null && !mFragment.isDetached()) {
            FragmentTransaction ft = fm.beginTransaction();
            ft.detach(mFragment);
            ft.commit();
        } 
    } 
 
    public void onTabSelected(Tab tab, FragmentTransaction unused) {
        // The parameter is always null 
        // See: http://groups.google.com/group/actionbarsherlock/browse_thread/thread/89eac58c13fe1ae0/8d6db0ba248e53d9?show_docid=8d6db0ba248e53d9 
        FragmentManager fm = mActivity.getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        if (mFragment == null) {
            mFragment = Fragment.instantiate(mActivity, mClass.getName(), mArgs);
6
CodeRank
This code example shows how to use the following methods:beginTransaction, findFragmentById
        UIHelp.setupActionBar(this);
 
        FragmentManager fm = getSupportFragmentManager();
 
        // Create the list fragment and add it as our sole content. 
        if (fm.findFragmentById(android.R.id.content) == null) {
            AgenciesFragment list = new AgenciesFragment();
            fm.beginTransaction().add(android.R.id.content, list).commit();
        } 
    } 
 
    @Override 
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            NavHelp.goHome(this);
            return true; 
        } 
        return false; 
    } 
} 
6
CodeRank
This code example shows how to use the following methods:setRetainInstance
  public ContentFragment() { 
    this(null); 
  } 
   
  public ContentFragment(String url) {
    super(); 
     
    urlToLoad=url;
    setRetainInstance(true);
  } 
   
  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                            Bundle savedInstanceState) {
    return(inflater.inflate(R.layout.content_fragment, container, false));
  } 
   
  @Override 
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
10
CodeRank
This code example shows how to use the following methods:
/** 
 * This demonstrates how you can implement switching between the tabs of a 
 * TabHost through fragments.  It uses a trick (see the code below) to allow 
 * the tabs to switch between fragments instead of simple views. 
 */ 
public class FragmentTabs extends FragmentActivity implements LocationListener{
    TabHost mTabHost;
    TabManager mTabManager;
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        setContentView(R.layout.fragment_tabs); 
        mTabHost = (TabHost)findViewById(android.R.id.tabhost);
        mTabHost.setup();
        mTabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);
        mTabManager = new TabManager(this, mTabHost, R.id.realtabcontent);
         
        setupTab("Dashboard", DashFrag.class); 
10
CodeRank
This code example shows how to use the following methods:beginTransaction
        startFragment(new FragmentActivity(), fragment);
    } 
	 
	public static void startFragment( FragmentActivity activity, Fragment fragment ) {
		FragmentManager fragmentManager = activity.getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add( fragment, null );
        fragmentTransaction.commit();
	} 
 
} 
6
CodeRank
This code example shows how to use the following methods:
 
import com.newsblur.R; 
import com.newsblur.view.StateToggleButton; 
import com.newsblur.view.StateToggleButton.StateChangedListener; 
 
public class FeedIntelligenceSelectorFragment extends Fragment implements StateChangedListener {
	 
	public static final String FRAGMENT_TAG = "feedIntelligenceSelector";
	private StateToggleButton button;
	 
	@Override 
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_intelligenceselector, null);
		button = (StateToggleButton) v.findViewById(R.id.fragment_intelligence_statebutton);
		button.setStateListener(this);
		return v;
	} 
 
	@Override 
	public void changedState(int state) {
10
CodeRank
This code example shows how to use the following methods:beginTransaction, findFragmentByTag
            super.onActivityCreated(savedInstanceState);
 
            FragmentManager fm = getFragmentManager();
 
            // Check to see if we have retained the worker fragment. 
            mWorkFragment = (RetainedFragment)fm.findFragmentByTag("work");
 
            // If not retained (or first time running), we need to create it. 
            if (mWorkFragment == null) {
                mWorkFragment = new RetainedFragment();
                // Tell it who it is working with. 
                mWorkFragment.setTargetFragment(this, 0);
                fm.beginTransaction().add(mWorkFragment, "work").commit();
            } 
        } 
 
    } 
 
    /** 
     * This is the Fragment implementation that will be retained across 
6
CodeRank
This code example shows how to use the following methods:
package net.avh4.tasks.android; 
 
import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import net.avh4.ide.example.net.avh4.tasks.android.R; 
 
/** 
 * An activity representing a list of Tasks. 
 */ 
public class TaskListActivity extends FragmentActivity { 
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_task_list); 
    } 
} 

10
CodeRank
This code example shows how to use the following methods:
import org.androidannotations.annotations.OptionsMenu; 
import org.androidannotations.test15.R; 
 
@EFragment 
@OptionsMenu({R.menu.my_menu, R.menu.my_menu2}) 
public class OptionsMenuSupportFragment extends Fragment { 
	 
	@OptionsItem 
	void menuRefreshSelected() { 
	} 
 
	@OptionsItem({ R.id.menu_search, R.id.menu_share }) 
	boolean multipleMenuItems() { 
		return false; 
	} 
 
	@OptionsItem 
	void menu_add(MenuItem item) { 
	} 
 
10
CodeRank
This code example shows how to use the following methods:
package mobi.tongari.mokutan.ui; 
 
import mobi.tongari.mokutan.R; 
import android.support.v4.app.FragmentActivity; 
 
import com.google.android.gms.maps.SupportMapFragment; 
import com.googlecode.androidannotations.annotations.EActivity; 
import com.googlecode.androidannotations.annotations.FragmentById; 
 
@EActivity(R.layout.activity_test_fragment) 
public class TestFragmentActivity extends FragmentActivity { 
 
@FragmentById 
ExMapFragment fragment1; 
	 
@FragmentById 
SupportMapFragment mapFragment; 
	 
} 

10
CodeRank

Related Android FragmentManager Questions & Answers:

See Code Examples for Android FragmentManager Methods: