Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Removing fragments from an activity

Tags:

android

FragmantClass rSum = new FragmantClass(); getSupportFragmentManager().beginTransaction().remove(rSum).commit();        

I am trying to remove this fragment, when i load switch another fragment. The above fragment does not get removed. Here is the method i am calling to switch fragments.

public void switchContent(Fragment fragment) {     FragmantClass rSum = new FragmantClass();     getSupportFragmentManager().beginTransaction().remove(rSum).commit();     mContent = fragment;     getSupportFragmentManager()         .beginTransaction()         .replace(R.id.content_frame, fragment)         .commit();     getSlidingMenu().showContent(); } 
like image 441
Kevin Avatar asked Apr 07 '13 01:04

Kevin


People also ask

How do I remove a fragment from a transaction?

To remove a fragment from the host, call remove() , passing in a fragment instance that was retrieved from the fragment manager through findFragmentById() or findFragmentByTag() . If the fragment's view was previously added to a container, the view is removed from the container at this point.

What are fragments and activities?

Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Fragment is dependent on activity. It can't exist independently.

Are fragments destroyed when activity is destroyed?

As Fragment is embedded inside an Activity, it will be killed when Activity is killed. As contents of activity are first killed, fragment will be destroyed just before activity gets destroyed.


1 Answers

getSupportFragmentManager().beginTransaction(). remove(getSupportFragmentManager().findFragmentById(R.id.frame)).commit(); 

Try this, it should work.

public void switchContent(Fragment fragment) {     getSupportFragmentManager().beginTransaction().     remove(getSupportFragmentManager().findFragmentById(R.id.frame)).commit();     mContent = fragment;     getSupportFragmentManager()         .beginTransaction()         .replace(R.id.content_frame, fragment)         .commit();     getSlidingMenu().showContent(); } 
like image 106
Thalaivar Avatar answered Oct 04 '22 06:10

Thalaivar