i have Fragment Activity With contatin navigation drawer.after clicking navigation drawer item it open a Fragment 1, fragment 2, Fragment 3.and then after button click event
Override onBackPress() method and check that your fragement manager has backStack entry>0 if it has greater than 0 than do popback stack otherwise finish your activity..
if(getFramentManager().getBackStackEntryCount()>0){
// popback statck.
}
else{
// finish your activity.
}
don't forget to add your fragement to BackStack to maintain fragment hierarchy..
fragTransacion.addToBackStack(TAG);
Your code for adding fragment to backstack should be like:
getFragmentManager() .beginTransaction() .replace(R.id.content_frame, Entry_Account.newInstance(), Entry_Account.TAG).addToBackStack("Some string") .commit();
Then On Activity's onBackPressed method,use this snippet:
@Override
public void onBackPressed () {
if(getFramentManager().getBackStackEntryCount()>0){
// popback statck.
}
else{
// finish your activity.
}
}
Add Fragments To BackStack. Before commit() the transaction, use addToBackStack() method i.e
addToBackStack("Some String").commit();
and in onBackPressed()
@Override
public void onBackPressed() {
if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
getSupportFragmentManager().popBackStack();
} else {
this.finish();
}
}