Jui*_*iCe 7 android actionbarsherlock android-actionbar
使用找到的开发人员指南here,我试图让我的图标导航回我的主屏幕.我目前有一个按钮来执行此操作,并复制并粘贴onOptionsItemSelected()方法中的代码.然而,点击图标永远不会做任何事情.这是ActionBar和ActionBarSherlock的不同之处吗?
这是作为示例给出的代码:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// app icon in action bar clicked; go home
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的代码:
public boolean onOptionsItemSelected( MenuItem item ) {
switch( item.getItemId() ) {
case R.id.mainTopBluetoothState:
Toast.makeText( this, "BluetoothState", Toast.LENGTH_SHORT ).show();
return true;
case R.id.mainTopAppState:
Toast.makeText( this, "BluetoothState", Toast.LENGTH_SHORT ).show();
return true;
case android.R.id.home:
Log.i( "In Home", "In Home" );
killToasts();
dispatchKeyEvent(new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK ));
finish();
return true;
}
return super.onOptionsItemSelected( item );
}
Run Code Online (Sandbox Code Playgroud)
当我点击图标时,没有任何反应.Log代码中的调用并没有在我的LogCat任何一个中显示.
您可能无法启用ABS活动徽标.添加此内容onCreate()
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Run Code Online (Sandbox Code Playgroud)
此外,如果您还没有这样做,请阅读实施祖先导航,以便您正确导航(忽略他们的使用getActionBar(),他们不使用ABS,这是实际的Android API Action Bar方法).