解决方法
i’d like to avoid parent activity being destroyed whenever i clicked < button in action bar.
按下该按钮后,您的父活动将暂停,而不是被销毁.
is there a way for me to override it? and is there a way to override for all activities?
只需致电:
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setHomeButtonEnabled(true);
您需要将其调用到要覆盖按钮的活动.没有其他选择可以称之为“一劳永逸”.
which method is being called from AppCompatActivity when we press that button?
默认情况下,它没有方法.如果您使用工具栏,请在按下该按钮时调用setNavigationOnClickListener()来执行某些操作.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); toolbar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // do something here,such as start an Intent to the parent activity. } });
如果您使用ActionBar,该按钮将在菜单上可用:
@Override public boolean onCreateOptionsMenu(Menu menu) { return super.onCreateOptionsMenu(menu); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == android.R.id.home) { // do something here,such as start an Intent to the parent activity. } return super.onOptionsItemSelected(item); }