“Back button” using getSupportActionbar and appcompat v7 toolbar

前端 未结 8 605
时光取名叫无心
时光取名叫无心 2021-01-30 02:27

I\'m using the new toolbar from the Appcompat V7 library and I\'m making an application with navigation drawer and with fragments.

In some fragments I don\'t want to sho

8条回答
  •  梦谈多话
    2021-01-30 02:58

    Add this method in onCreate():

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    Then override the onOptionItemSelected() as below:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    

提交回复
热议问题