Answers for "on back pressed in fragment"

5

how to handle onbackpressed in fragment

requireActivity().onBackPressedDispatcher.addCallback(viewLifecycleOwner){
	YOU CODE !!
}
Posted by: Guest on November-09-2021
3

onBackPressed() in fragment

activity?.onBackPressedDispatcher?.addCallback(viewLifecycleOwner, object : OnBackPressedCallback(true) {
            override fun handleOnBackPressed() {

            }
        })
Posted by: Guest on October-27-2021
0

on back press in fragment

//You need to add the following line for this solution to work; thanks skayred
fragment.getView().setFocusableInTouchMode(true);
fragment.getView().requestFocus();
fragment.getView().setOnKeyListener( new OnKeyListener()
{
    @Override
    public boolean onKey( View v, int keyCode, KeyEvent event )
    {
        if( keyCode == KeyEvent.KEYCODE_BACK )
        {
            return true;
        }
        return false;
    }
} );
Posted by: Guest on June-14-2021

Browse Popular Code Answers by Language