Add back button to action bar

后端 未结 11 1716
野趣味
野趣味 2020-12-07 16:23

I have been trying to add a back button to the action bar.

I want my view to look like this: \"enter

11条回答
  •  北荒
    北荒 (楼主)
    2020-12-07 17:07

    Use this to show back button and move to previous activity,

    final ActionBar actionBar = getSupportActionBar();
            assert actionBar != null;
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.back_dark);
    
    
    @Override
        public boolean onOptionsItemSelected(final MenuItem item) {
    
            switch (item.getItemId()) {
                case android.R.id.home:
                    onBackPressed();
                    return true;
                default:
                    return super.onOptionsItemSelected(item);
            }
        }
    

提交回复
热议问题