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(); }
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.
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.
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.
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(); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With