Action Bar's onClick listener for the Home but

2019-01-30 05:35发布

How can I implement a custom onClickListener for the Home button of the Action Bar?

I already did a getSupportActionBar().setDisplayHomeAsUpEnabled(true); and now I want to redirect the user to a certain activity in case the Home button is clicked.

I tried with:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            item.setOnMenuItemClickListener(new OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    Intent i = new Intent();
                    i.setClass(BestemmingActivity.this, StartActivity.class);
                    i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(i);
                    return true;
                }
            });
        default:
            return super.onOptionsItemSelected(item);
        }
    }

but it never enters in the onMenuItemClick.

Basically, it's done just like in this link but still it doesn't enter in the listener.

8条回答
够拽才男人
2楼-- · 2019-01-30 06:33

Best way to customize Action bar onClickListener is onSupportNavigateUp()

This code will be helpful link for helping code

查看更多
贼婆χ
3楼-- · 2019-01-30 06:34

Fixed: no need to use a setOnMenuItemClickListener. Just pressing the button, it creates and launches the activity through the intent.

Thanks a lot everybody for your help!

查看更多
登录 后发表回答