评估R.id.home

use*_*524 5 android constants

我在这里遵循这个教程 - http://developer.android.com/training/implementing-navigation/ancestral.html - 用于实现向上导航.与用户按下电话上的"后退"按钮的排序相同,但按下"向上"按钮时,onBackPressed()方法不会触发.在教程中,它们显示您在onOptionsItemSelected()方法中捕获R.id.home.这个网页 - http://developer.android.com/reference/android/R.id.html - 显示R.id.home的值应该等于16908332,但它不在我的应用程序中.在下面的代码中,如果我使用R.id.home它会失败.如果我在16908332中进行硬编码就行了.对我来说,R.id.home的计算结果为21312330724.根据该页面,所有的R.id值都从1690开始.我讨厌在内置值的值中进行硬编码,但我不知道还有什么其他的做.这可能导致问题吗?难道我做错了什么?这是一个错误吗?

格雷格

@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();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }
    //noinspection SimplifiableIfStatement
    if (id == R.id.home) {//16908332
        Intent upIntent = NavUtils.getParentActivityIntent(this);

        upIntent.putExtra(CAT_ID, CatID);
        upIntent.putExtra(USER_ID, UserID);
        upIntent.putExtra(LIST_ID, ListID);
        setResult(RESULT_OK, upIntent);
        NavUtils.navigateUpTo(this, upIntent);
        return true;
    }

    return super.onOptionsItemSelected(item);
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*cSB 27

你需要将它与android的比较:

if (id == android.R.id.home){
    ...
}
Run Code Online (Sandbox Code Playgroud)