I\'m using the built-in navigation drawer to run my app. I can\'t quite figure out how to handle the back button. When it\'s pressed I want it to load the very first fragmen
Instead of:
fm.beginTransaction().replace(R.id.main, newFragment).addToBackStack("fragBack").commit();
Call:
fm.beginTransaction().add(R.id.main, newFragment).addToBackStack("fragBack").commit();
addToBackStack works with add
.
replace
function removes previous fragment and places new fragment so on your back-stack there is only one fragment all the time. So use add function to keep previous fragments on stack.
To always goto fragemnt1 from any fragment onBackPress try to do following:
getFragmentManager().popBackStack();
fm.beginTransaction().add(R.id.main, newFragment).addToBackStack("fragBack").commit();
this will remove last transaction from backstack and add new one. Try this.