Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreferenceActivity actionbar home icon won't return home (unlike ET :)

My PreferenceActivity works great except for one thing. The ActionBar icon, which perfectly returns the user to the previous activity in all my other activities doesn't work in the PreferenceActivity. When I click the icon it flashes as if it was going to return to the previous activity but the PreferenceActivity stays on screen. Interestingly the back button does return the user to the previous activity. Is there a way to make the ActionBar's Home icon work "normally" in the PreferenceActivity?

Here is the code:

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
  // Set actionBar controls for Settings
    TextView actionBarTitle = (TextView) findViewById(Resources.getSystem().getIdentifier("action_bar_title", "id", "android"));
    ActionBar actionBar = getActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);  
    actionBar.setIcon(R.drawable.ic_launcher); 
    actionBar.setDisplayShowTitleEnabled(true); 
    actionBarTitle.setTextColor(Color.WHITE);
    actionBarTitle.setTextSize(16); 
    actionBar.setTitle(R.string.settings_menu_title);

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {  //Build.VERSION_CODES.ICE_CREAM_SANDWICH
        actionBar.setHomeButtonEnabled(true); 
        actionBar.setDisplayHomeAsUpEnabled(true); // show back arrow on title icon
        actionBar.setDisplayShowHomeEnabled(true);
    }
...... Handle prefs (all working fine).....
   }

}

//////And the calling code ////////

 //Use menu button to access settings screen
    @Override
   public boolean onKeyDown(int keycode, KeyEvent e) {
       switch(keycode) {
        case KeyEvent.KEYCODE_MENU:
            Intent intent = new Intent(this, SettingsActivity.class);
            startActivity(intent);
            return true;
       }
       return super.onKeyDown(keycode, e);
    }  // [END onKeyDown (for menu click capture) ]
like image 1000
PeteH Avatar asked Mar 02 '13 07:03

PeteH


1 Answers

Thanks to @Axarydax for pointing me in the right direction. I came to realize that PreferenceActivity is different from my other activities since the Home button returns to the calling Activity instead of to the MainActivity. Therefore the solution required (1) using startActivityForResult (instead of startActivity) to call the PreferenceActivity and (2) using onOptionsItemSelected in the PreferenceActivity to manage the return (per @Axarydax's answer). Both (1) and (2) are shown below:

 // (1) Menu button used to access PreferenceActivity
 @Override
  public boolean onKeyDown(int keycode, KeyEvent e) {
      switch(keycode) {
        case KeyEvent.KEYCODE_MENU:
          Intent intent = new Intent(this, SettingsActivity.class);
          startActivityForResult(intent, 1);  //enables return to here
          return true;
        }
      return super.onKeyDown(keycode, e);
  }  

 // (2) Return to calling activity from PreferenceActivity
   @Override
 public boolean onOptionsItemSelected(MenuItem item) {
     if (item.getItemId() == android.R.id.home) {
         int SUCCESS_RESULT=1;
         setResult(SUCCESS_RESULT, new Intent());
         finish();  //return to caller
         return true;
     }
     return false;
 }
like image 128
PeteH Avatar answered Sep 21 '22 14:09

PeteH