Action Bar detect back button click in fragment

后端 未结 3 501
独厮守ぢ
独厮守ぢ 2021-01-21 06:30

I have a Fragment with action bar back button enabled in it.

Code :

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activi         


        
3条回答
  •  长情又很酷
    2021-01-21 07:08

    I was facing the same problem, but then I put this code inside the activity and the back button worked from inside the fragment:

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                Toast.makeText(getActivity(), "Back", Toast.LENGTH_LONG).show();
                break;
            default:
                break;
            }
            return super.onOptionsItemSelected(item);
        }
    

提交回复
热议问题