0

I have a few fragments in one activity. In Main Activity I implemented popBackStack(); in onBackPressed();

So that, you can always go back to previous fragment clicking back button. But popBackStack(); doesn't remove the fragment I just left.

How to achieve removing current fragment each time, we click back button?

Tomek
  • 386
  • 1
  • 4
  • 11
  • How do you load the fragments? Do you call addToBackStack()? If so you don't need to implement onBackPressed() as Android does this for you. – Code-Apprentice Sep 24 '16 at 14:02
  • As told by code-apprentice, if you properly add fragment during fragment transaction. Then you need not pop out the fragment specifically. popbackStack is generally used for buttons and clicks other than back button – Mohammed Atif Sep 24 '16 at 14:10
  • look at the answer here: http://stackoverflow.com/questions/34025331/how-can-i-control-the-activitys-up-button-from-a-contained-fragment – Daniel Nugent Sep 24 '16 at 15:20

1 Answers1

4
 public void removeFragmentbyTag(String myFrag){

 FragmentManager manager = getActivity().getSupportFragmentManager();
 FragmentTransaction trans = manager.beginTransaction();
 trans.remove(myFrag);
 trans.commit();
 manager.popBackStack();
}

hope this bit of code help you.

Vij
  • 445
  • 2
  • 9