简体   繁体   English

Android操作栏主页按钮

[英]Android action bar home button

I've set my action bar like so but nothing happens when I click the home up button. 我已经设置了我的操作栏,但是当我点击主页按钮时没有任何反应。 The two options below are enabled so shouldn't it go to the home activity automatically? 以下两个选项已启用,因此不应自动转到主页活动?

ab.setHomeButtonEnabled(true);
ab.setDisplayHomeAsUpEnabled(true);

Action Bar 行动吧

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);

    final String[] activities;
    Resources res = getResources();
    activities =  res.getStringArray(R.array.activities);

    ActionBar ab = getActionBar();
    ab.setHomeButtonEnabled(true);
    ab.setDisplayHomeAsUpEnabled(true);
    ab.setTitle(R.string.app_name);
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    ab.show();

    /** Create an array adapter to populate dropdownlist */
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, activities);

    /** Setting dropdown items and item navigation listener for the actionbar */
    getActionBar().setListNavigationCallbacks(adapter, navigationListener);

    /** Enabling dropdown list navigation for the action bar */
    getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    return true;
}

As other people have said, the behaviour doesn't happen automatically - you need to tell it where to go. 正如其他人所说,这种行为不是自动发生的 - 你需要告诉它去哪里。

However, I need to add another answer, as the current answers are all breaking Android design guidelines - Back != Home. 但是,我需要添加另一种答案,因为目前的答案打破Android的设计指南! -返回=家庭。 See the documentation 请参阅文档

What you really want to be doing is something along the lines of this: 你真正想要做的是这样的事情:

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case android.R.id.home:
            Intent homeIntent = new Intent(this, HomeActivity.class);
            homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(homeIntent);
    }
    return (super.onOptionsItemSelected(menuItem));
}

Which will take you to the parent activity, rather than just go through the back stack. 这将带您进入父活动,而不仅仅是通过后台堆栈。 I've also added the Intent.Flag to clear the back stack, it's a useful one to have when going to a home activity and can stop the back stack getting in a muddle when your users are using the 'Up' button 我还添加了Intent.Flag以清除后台堆栈,当你进入家庭活动时它是一个很有用的,并且当你的用户使用“向上”按钮时可以阻止后台陷入困境

You also need to make sure your App knows what to do when it is pressed: 您还需要确保您的应用程序在按下时知道该怎么做:

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    case android.R.id.home:
      // ProjectsActivity is my 'home' activity
      super. onBackPressed();
      return true;
    }
  return (super.onOptionsItemSelected(menuItem));
}

We have to define meta data into our child activity in AndroidManifast.xml file as per given in official document : 我们必须按照官方文档中的规定将元数据定义到AndroidManifast.xml文件中的子活动:

<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
    android:name="com.example.myfirstapp.MainActivity" ...>
    ...
</activity>
<!-- A child of the main activity -->
<activity
    android:name="com.example.myfirstapp.DisplayMessageActivity"
    android:label="@string/title_activity_display_message"
    android:parentActivityName="com.example.myfirstapp.MainActivity" >
    <!-- The meta-data element is needed for versions lower than 4.1 -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>

There is no need to define meta data if version is 4.1 or above and you have to enable your action bar home button as you have done in your code. 如果版本为4.1或更高版本,则无需定义元数据,您必须像在代码中一样启用操作栏主页按钮。 No need to use back button code and it is working fine with my android app: Helper+ 无需使用后退按钮代码,它与我的Android应用程序正常工作: Helper +

You need to define what happens here: 你需要定义这里发生的事情:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
      case android.R.id.home:
        onBackPressed();
    }
    return true;
}

You can achieve this using below one method although there are lots of ways to do so. 您可以使用以下方法实现此目的,尽管有很多方法可以实现。

put this line inside your onCreate 把这一行放在你的onCreate

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

override method onSupportNavigateUp in your activity 在您的活动中覆盖onSupportNavigateUp方法

@Override
public boolean onSupportNavigateUp() {
    finish();
    return super.onSupportNavigateUp();
}

Add these lines in your code: 在代码中添加以下行:

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {

    case android.R.id.home:
        System.out.println("Pressed Back Button");
        break;

    default:
        return super.onOptionsItemSelected(item);
    }
    return false;
}

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

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