我有一个包含searchview的动作条。当用户单击搜索按钮并折叠搜索视图时,操作栏在左侧显示一个后退按钮。

当用户单击“后退”按钮时,我们如何检测?
编辑
根据答案,我检查了我的OnOptionsItemSelected,但它没有调用。这是我的OnOptionsItemSelected代码
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (item != null && id == android.R.id.home) {
if (mNavigationDrawerFragment.isDrawerOpen(Gravity.RIGHT)) {
mNavigationDrawerFragment.closeDrawer(Gravity.RIGHT);
} else {
mNavigationDrawerFragment.openDrawer(Gravity.RIGHT);
}
return true;
}
if (id == R.id.action_search) {
return true;
}
return super.onOptionsItemSelected(item);
}发布于 2014-10-15 09:54:12
您应该为您的manifest.xml添加元数据,以便进行您想要的活动。
喜欢
<activity
android:name=".Example"
android:label="@string/Example"
android:theme="Theme.AppCompat.Light">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>例如,您的代码应该如下所示
@Override
protected void onCreate(Bundle savedInstanceState) {
.......
getActionBar().setDisplayHomeAsUpEnabled(true);
......
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
// Respond to the action bar's Up/Home button
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}https://stackoverflow.com/questions/26376429
复制相似问题