override onbackpressed in fragment

Solutions on MaxInterview for override onbackpressed in fragment by the best coders in the world

showing results for - "override onbackpressed in fragment"
Nolan
22 Nov 2019
1@Override
2public void onResume() {
3    super.onResume();
4
5    getView().setFocusableInTouchMode(true);
6    getView().requestFocus();
7    getView().setOnKeyListener(new View.OnKeyListener() {
8        @Override
9        public boolean onKey(View v, int keyCode, KeyEvent event) {
10            if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
11                // handle back button's click listener
12                Toast.makeText(getActivity(), "Back press", Toast.LENGTH_SHORT).show();
13                return true;
14            }
15            return false;
16        }
17    });
18
19}
20