Android的操作栏主页按钮 [英] Android action bar home button

查看:261
本文介绍了Android的操作栏主页按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设置我的行动吧,像这样,但没有任何反应,当我点击了首页上按钮。 下面启用,因此这两个选项应该不会,它会自动转到主页活动呢?

  ab.setHomeButtonEnabled(真正的);
ab.setDisplayHomeAsUpEnabled(真正的);
 

操作栏

  @覆盖
公共布尔onCreateOptionsMenu(功能菜单){
    MenuInflater充气= getMenuInflater();
    inflater.inflate(R.menu.menu,菜单);

    最终的String []的活动;
    资源RES = getResources();
    活动= res.getStringArray(R.array.activities);

    动作条AB = getActionBar();
    ab.setHomeButtonEnabled(真正的);
    ab.setDisplayHomeAsUpEnabled(真正的);
    ab.setTitle(R.string.app_name);
    ab.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    ab.show();

    / **创建一个数组适配器来填充的DropDownList * /
    ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(getBaseContext(),android.R.layout.simple_spinner_dropdown_item,活动);

    / **设置下拉列表项和项目导航侦听器的动作条* /
    。getActionBar()setListNavigationCallbacks(适配器,navigationListener);

    / **启用下拉列表导航操​​作栏* /
    。getActionBar()setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
    返回true;
}
 

解决方案

至于其他人说,该行为不会自动发生 - 你需要告诉它去哪里

不过,我需要添加另一个答案,因为目前的答案是的所有的突破Android的设计准则 - 返回=家庭。请参见的文档

你真正想要做的是什么沿着这行的东西:

  @覆盖
公共布尔onOptionsItemSelected(菜单项菜单项){
    开关(menu​​Item.getItemId()){
        案例android.R.id.home:
            意图homeIntent =新的意图(这一点,HomeActivity.class);
            homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(homeIntent);
    }
    返程(super.onOptionsItemSelected(菜单项));
}
 

,将带你到父活动,而不是仅仅通过后面的堆栈。我还添加了 Intent.Flag 清除后退堆栈,这是一个有益的去一个家庭活动时,并能阻止后面堆得到一个懵懵懂懂地有如果用户使用的是向上按钮

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. 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

这篇关于Android的操作栏主页按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆