返回按钮没有正确响应
问题描述:
这真的很奇怪,很难解释。我在活动覆盖onOptionsItemSelected
,但它只是开始起作用了一定片段开始后,我真的不明白这一点...返回按钮没有正确响应
活动
@Override
public boolean onOptionsItemSelected(MenuItem item) {
Log.e("hm", ""+ item.getItemId());
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(Activity_Main.this, "back", Toast.LENGTH_SHORT).show();
onBackPressed();
return true;
case R.id.settings_toolbar:
Toast.makeText(Activity_Main.this, "setings", Toast.LENGTH_SHORT).show();
toSettings();
return true;
case R.id.share_toolbar:
Toast.makeText(getApplicationContext(), "shared lmao", Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
开始没有响应片段到工具栏动作:
public void toSettings() {
Fragment_Settings frag = new Fragment_Settings();
fm.beginTransaction()
.replace(R.id.mainContainer, frag)
.addToBackStack(null)
.commit();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.arrow_left);
}
请别人帮忙。
答
对于所有有这种令人困扰的经历的人,出于某种原因,您必须为onCreateOptionsMenu()
充气菜单才能使后退按钮正常工作。