Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clicking app icon doesn't trigger onOptionsItemSelected()

Tags:

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" > 
like image 557
Nait Avatar asked Jan 21 '12 12:01

Nait


1 Answers

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); } 
like image 129
David Snabel-Caunt Avatar answered Oct 04 '22 21:10

David Snabel-Caunt