Add this method to your activity:
@Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { if( this.getFragmentManager().getBackStackEntryCount() != 0 ){ this.getFragmentManager().popBackStack(); return true; }
Then, when you change the fragments, do the following:
FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(android.R.id.content, new YourFragmentName()); transaction.addToBackStack(null); // this is needed for the above code to work transaction.commit();
Adil malik
source share