I am using Android Sliding Menu using Navigation Drawer in my application and Fragments are used in the app instead of Activities. When I open the drawer, click on an item a Fra
Try these methods. To me, the most useful solution is as follows:
In MainActivity:
getSupportFragmentManager().beginTransaction().replace(R.id.gif_contents, gifPageTwoFragment, "gifPageTwoFragment").addToBackStack("gifPageTwoFragment").commit();
In GifPageTwoFragment:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
Log.e("gif--","fragment back key is clicked");
getActivity().getSupportFragmentManager().popBackStack("gifPageTwoFragment", FragmentManager.POP_BACK_STACK_INCLUSIVE);
return true;
}
return false;
}
});
}