我目前正在开发一个 Android 应用程序。我想使用操作栏中的应用程序图标导航到“主页”活动。我在此页面上读到,需要做的就是添加onOptionsItemSelected
并查找 id android.R.id.home
。
这是我在活动中实现的代码,我想按下应用程序图标返回到HomeActivity
.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
然而,什么也没有发生。调试时,我可以看到单击图标根本不会触发onOptionsItemSelected()
。我必须在某处对图标做些什么吗?截至目前,它都是默认的,只是这个在AndroidManifest.xml
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >