Android - Back button in the title bar

前端 未结 26 2323
南方客
南方客 2020-12-04 07:02

In many apps (Calendar, Drive, Play Store) when you tap a button and enter a new activity, the icon in the title bar turns into a back button, but for the app I am making, i

26条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-04 07:38

    I have finally managed to properly add back button to actionbar/toolbar

    @Override
    public void onCreate(Bundle savedInstanceState) {
        ...
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }  
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    public boolean onCreateOptionsMenu(Menu menu) {
        return true;
    }
    

提交回复
热议问题