KeyListener snippet not working

I am trying to end my activity with fragment BackPressed. But the tag Key Listeneris not called at all. I searched for his problem EditText, most of the time. This is my code.

public class ProductFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.product_fragment, container,false);


        view.setOnKeyListener( new View.OnKeyListener() {

            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // TODO Auto-generated method stub

                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    getActivity().finish();
                    return true;
                }
                return false;
            }
        });
     }
+4
source share
1 answer

Perhaps you can just override onBackPressed () in the holding activity. It will give the result you are looking for. Check this out: http://developer.android.com/reference/android/app/Activity.html#onBackPressed ()

+1
source

Source: https://habr.com/ru/post/1540032/


All Articles