Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Action Bar detect back button click in fragment

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

Code :

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        setHasOptionsMenu(true);
        actionBar = ((MainActivity)getActivity()).getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);

        actionBar.setCustomView(R.layout.custom_action_bar);
        actionBar.setDisplayShowTitleEnabled(false);
        actionBar.setDisplayShowCustomEnabled(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.fragment_layout, container, false);

        return view;
    }

In the above code actionBar.setDisplayHomeAsUpEnabled(true); enables the back button in the Action Bar but how can we detect the click on it ?

I have looked into many examples and tried the below but still not working :

In 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);
    }

Also the back button which is displayed in the fragment is of Black color i need that in White color or with custom color how can i change that as well ?

like image 284
user2056563 Avatar asked Sep 20 '25 17:09

user2056563


2 Answers

Better late than never, please try this>

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        mInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = mInflater.inflate(R.layout.fragment_layout, container, false);
        //set setHasOptionsMenu on true here on CreateView method
        setHasOptionsMenu(true);
        return view;
    }

and implement

@Override
public boolean onOptionsItemSelected(MenuItem item){
            if (item.getItemId() == android.R.id.home) {
                if (getActivity() != null) {
                    getActivity().onBackPressed();
                }
                return true;
            };
            return super.onOptionsItemSelected(item);
 }
like image 59
JoCuTo Avatar answered Sep 22 '25 09:09

JoCuTo


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);
    }
like image 31
Malek Hijazi Avatar answered Sep 22 '25 07:09

Malek Hijazi