12

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 Fragment appears. I move from one fragment to another fragment using the following code:

Fragment fragment = null;
fragment = new GalleryFragment(selectetdMainMenu.getCategoryID());
                    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.addToBackStack("menuFrag");
                    ft.add(R.id.frame_container, fragment, "menuFrag");
                    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                    ft.commit();

In this way I can go from one fragment to another but I fail to come to the previous fragment on back button press. I managed to come up with this code to handle back press in MainActivity where Drawer is Initialized:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    super.onKeyDown(keyCode, event);
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        Fragment fragment_byTag = fragmentManager.findFragmentByTag("menuFrag");
        Fragment menuFragment_by_tag = fragmentManager.findFragmentByTag("galleryFrag");
        Fragment commentsFrag_by_tag = fragmentManager.findFragmentByTag("commentsFrag");
        Fragment dealDetail = fragmentManager.findFragmentByTag("promoFrag");
            if(commentsFrag_by_tag != null){
                if (commentsFrag_by_tag.isVisible()) {
                    Log.e("comments back  ", " clicked");
                    //menuDetailsFrag.onBackPressed();
                    FragmentManager fragmentManager = getSupportFragmentManager();
                    fragmentManager.beginTransaction().remove(commentsFrag_by_tag).commit();
                    fragmentManager.beginTransaction().show(menuFragment_by_tag).commit();
                }
            }else if(menuFragment_by_tag.isVisible()){
                Log.e("menu back  ", " clicked");
                menuDetailsFrag.onBackPressed();
                FragmentManager fragmentManager = getSupportFragmentManager();
                fragmentManager.beginTransaction().remove(menuFragment_by_tag).commit();
                fragmentManager.beginTransaction().show(fragment_byTag).commit();
            }
        }



    return false;
}

This works at times but fails most of the time. I would greatly appreciate if a better way to navigate back can be shown.

TharakaNirmana
  • 10,237
  • 8
  • 50
  • 69
  • Check this one out .. http://stackoverflow.com/questions/22552653/how-to-implement-onbackpressed-intents-in-fragment – J.Ajendra Mar 21 '14 at 07:45
  • 1
    add this `ft.addToBackStack(null);ft.commit();` add the fragments to the backstack and pop them. http://stackoverflow.com/questions/22550420/how-to-handle-back-event-in-drawer-list-fragment-in-android-activity/22550460#22550460 – Raghunandan Mar 21 '14 at 07:45
  • @Raghunandan. The easiest solution I found is yours. – YuDroid Nov 29 '14 at 12:21
  • I know this post is old but see my answer: https://stackoverflow.com/a/45430033/3999826 – Hamidreza Samadi Nov 29 '17 at 13:16

5 Answers5

60

I usually set an onKeyListener to the View in onResume. From what I learned you have to take care to set setFocusableInTouchMode() and requestFocus on the View.

This is a sample of what I use for this purpose:

@Override
public void onResume() {

    super.onResume();

    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){

                // handle back button

                return true;

            }

            return false;
        }
    });
}
super-qua
  • 3,148
  • 1
  • 23
  • 30
5

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;
            }
        });
    }
Matt Tester
  • 4,663
  • 4
  • 29
  • 32
renhui
  • 51
  • 1
  • 2
2

In your oncreateView() method you need to write this code and in KEYCODE_BACk condition you can write whatever the functionality you want

   View v = inflater.inflate(R.layout.xyz, container, false);
    //Back pressed Logic for fragment
    v.setFocusableInTouchMode(true);
    v.requestFocus();
    v.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    getActivity().finish();
                    Intent intent = new Intent(getActivity(), MainActivity.class);
                    startActivity(intent);

                    return true;
                }
            }
            return false;
        }
    });
Raj Kumar
  • 688
  • 8
  • 18
0
        view.setFocusableInTouchMode(true);
        view.requestFocus();
        view.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event)   {
        if (keyCode == KeyEvent.KEYCODE_BACK) {


            Fragment NameofFragment = new NameofFragment;

            FragmentTransaction  transaction=getFragmentManager().beginTransaction();
            transaction.replace(R.id.frame_container,NameofFragment);

            transaction.commit();

            return true;
        }
        return false;
    }
});

return view;
Ashish Soni
  • 286
  • 3
  • 4
0

use this (in kotlin)

activity?.onBackPressedDispatcher?.addCallback(this, object : OnBackPressedCallback(true) {
    override fun handleOnBackPressed() {
        // in here you can do logic when backPress is clicked
    }
})

i think this is the most elegant way to do it

Abhilash Das
  • 1,388
  • 1
  • 16
  • 22