击应用程序图标不会触发onOptionsItemSelected

击应用程序图标不会触发onOptionsItemSelected

本文介绍了点击应用程序图标不会触发onOptionsItemSelected()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前工作的一个Android应用程序。我想使用的应用程序图标的操作栏,导航到家的活动。我读了页面,所有需要做的是添加一个 onOptionsItemSelected 并查找ID android.R.id.home

这是我想要的preSS的应用程序图标,返回 HomeActivity ,我在我的活动已经实现了code。

@覆盖公共布尔onOptionsItemSelected(菜单项项){    开关(item.getItemId()){    案例android.R.id.home:        意向意图=新的意图(这一点,HomeActivity.class);        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);        startActivity(意向);        返回true;    默认:        返回super.onOptionsItemSelected(项目);    }}

然而,什么也没有发生。调试时,我可以看到,点击该图标不会触发 onOptionsItemSelected()可言。我必须做一些与地方的图标?截至目前,它的所有默认情况下,仅这在的Andr​​oidManifest.xml

<应用    机器人:图标=@可绘制/ ic_launcher    机器人:标签=@字符串/ APP_NAME>

解决方案

有关包目标定位API 14级开始,你需要通过调用<$c$c>setHomeButtonEnabled()

在您的onCreate,添加以下内容:

 如果(Build.VERSION.SDK_INT&GT; = Build.VERSION_ codeS.ICE_CREAM_SANDWICH){
    getActionBar()setHomeButtonEnabled(真)。
}
 

I'm currently working on an Android app. I would like to use the app icon in the action bar to navigate to the "home" activity. I read on this page that all that needs to be done is to add an onOptionsItemSelected and look for the id android.R.id.home.

This is the code that I have implemented in my activity where I want to press the app icon to return to HomeActivity.

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

However, nothing happens. When debugging, I can see that clicking the icon doesn't trigger the onOptionsItemSelected() at all. Do I have to do something with the icon somewhere? As of now, it's all default, just this in the AndroidManifest.xml

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
解决方案

For packages targetting API level 14 onwards, you need to enable the home button by calling setHomeButtonEnabled()

In your onCreate, add the following:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
    getActionBar().setHomeButtonEnabled(true);
}

这篇关于点击应用程序图标不会触发onOptionsItemSelected()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 11:09