Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using icon in top left of ActionBarSherlock to navigate

Using the developers guide found here, I am trying to make my icon navigate back to my home screen. I currently have a button which does this, and have copy and pasted the code in the onOptionsItemSelected() method. However tapping the icon never does anything. Is this a difference in ActionBar and ActionBarSherlock?

This is the code given as an example:

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

This is the code that I am using:

public boolean onOptionsItemSelected( MenuItem item ) {
    switch( item.getItemId() ) {
    case R.id.mainTopBluetoothState:
        Toast.makeText( this, "BluetoothState", Toast.LENGTH_SHORT ).show();
        return true;
    case R.id.mainTopAppState:
        Toast.makeText( this,  "BluetoothState",  Toast.LENGTH_SHORT ).show();
        return true;
    case android.R.id.home:
        Log.i( "In Home", "In Home" );
        killToasts();
        dispatchKeyEvent(new KeyEvent( KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK ));
        finish();
        return true;
    }
    return super.onOptionsItemSelected( item );
}

When I tap the icon, nothing happens. The Log call in the code isn't ever shown in my LogCat either.

like image 255
JuiCe Avatar asked Jan 23 '13 19:01

JuiCe


1 Answers

You are probably not enabling the ABS Activity logo to be clickable. Add this in onCreate()

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

Also, if you haven't done so already, read Implementing Ancestral Navigation so you navigate up properly (ignore their use of getActionBar(), they're not using ABS, and that's the actual Android API Action Bar method).

like image 91
A--C Avatar answered Oct 20 '22 04:10

A--C