简体   繁体   English

ActionBar中的Home Up按钮不起作用

[英]Home Up Button in ActionBar doesn't work

I've a problem with the Home Up Button in ActionBar . 我在ActionBar的Home Up按钮有问题。 I'm in a PreferenceActivity and in the onCreate i've put the following code: 我在PreferenceActivity ,在onCreate我输入了以下代码:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }

When I run my application it shows the Up Button but when I click on it nothing happens. 当我运行我的应用程序时,它显示了向上按钮,但是当我单击它时什么也没有发生。 So I put this code in the AndroidManifest : 所以我把这段代码放在AndroidManifest

    <activity android:name="com.example.mypackage.SystemInfo">
      <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.simonedev.androtools.MainActivity" />
    </activity>

But I still have the same problem. 但是我仍然有同样的问题。 How can I solve it? 我该如何解决? Where is the problem? 问题出在哪儿?

The wrong way up 错误的方式

You shouldn't use 你不应该使用

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch(item.getItemId()) {
        case android.R.id.home:
        //Do not use the following
        //PreferenceActivity.this.onBackPressed(); 
        //or
        //finish();
        break;
    }
    return true;   
}

The proper way up 正确的方法

But instead follow this presentation and google guidelines 但是,请遵循此演示文稿Google准则

The Manifest should add 1 more attribute 清单应再添加1个属性

<activity android:name="com.example.mypackage.SystemInfo"
    android:parentActivityName="com.simonedev.androtools.MainActivity" >
  <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.simonedev.androtools.MainActivity" />
</activity>

Copied code from the presentation : this would be the simple case 演示文稿中复制的代码:这将是简单的情况

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    super.onOptionsItemSelected(item);
    switch(item.getItemId()) {
        case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        break;
    }
    return true;   
}

I invite you to read the whole presentation for other more complex cases 我邀请您阅读整个演示文稿,以了解其他更复杂的情况

Add this method in your class, but outside the onCreate() method 在您的课程中添加此方法,但在onCreate()方法之外

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.action_bar, menu);
setTitle("");
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
 super.onOptionsItemSelected(item);
 switch(item.getItemId())
 {

case android.R.id.home:
PreferenceActivity.this.onBackPressed(); 
break;


 }
 return true;   
 }

Now, after your comments. 现在,在您发表评论之后。 you may have to create a file called action_bar.xml in your res/menu folder, and add the following to it 您可能需要在res / menu文件夹中创建一个名为action_bar.xml文件,并将以下内容添加到其中

 <menu xmlns:android="http://schemas.android.com/apk/res/android">
 </menu>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM