Android - Back button in the title bar

前端 未结 26 2417
南方客
南方客 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:25

    Light-weighted version without using ActionBarActivity that still has the same bahaviors here:

    public class ToolbarConfigurer implements View.OnClickListener {
        private Activity activity;
    
        public ToolbarConfigurer(Activity activity, Toolbar toolbar, boolean displayHomeAsUpEnabled) {
            toolbar.setTitle((this.activity = activity).getTitle());
            if (!displayHomeAsUpEnabled) return;
            toolbar.setNavigationIcon(R.drawable.abc_ic_ab_back_mtrl_am_alpha);
            toolbar.setNavigationOnClickListener(this);
        }
    
        @Override
        public void onClick(View v) {
            NavUtils.navigateUpFromSameTask(activity);
        }
    }
    

    Usage: Put new ToolbarConfigurer(this, (Toolbar) findViewById(R.id.my_awesome_toolbar), true); in onCreate.

提交回复
热议问题