Handling back press when using fragments in Android

前端 未结 5 838
一整个雨季
一整个雨季 2021-01-30 12:00

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

相关标签:
5条回答
  • 2021-01-30 12:24

    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;
            }
        });
    }
    
    0 讨论(0)
  • 2021-01-30 12:34

    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

    0 讨论(0)
  • 2021-01-30 12:36
            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;
    
    0 讨论(0)
  • 2021-01-30 12:43

    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;
            }
        });
    
    0 讨论(0)
  • 2021-01-30 12:49

    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;
                }
            });
        }
    
    0 讨论(0)
提交回复
热议问题