4

I have a SettingsActivity that should have an up button linking back to MainActivity. The button appears, but doesn't do anything when clicked. The back button works fine.

AndroidManifest.xml:

<activity
        android:name=".SettingsActivity"
        android:label="@string/title_activity_settings"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.myapp.MainActivity" />
</activity>

I tried adding an action to onOptionsItemSelected for R.id.home with no luck. I also tried adding getActionBar().setDisplayHomeAsUpEnabled(true); to onCreate(), but it crashes the app with the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.SettingsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)'
RedBassett
  • 3,469
  • 3
  • 32
  • 56
  • and have you tried `getSupportActionBar().setDisplayHomeAsUpEnabled(true);` ? – Shree Krishna Mar 27 '16 at 17:22
  • Please read the full question ;) – RedBassett Mar 27 '16 at 17:22
  • Yes I've already read it bro, and I am talking about `getSupportActionBar()` ? – Shree Krishna Mar 27 '16 at 17:24
  • Thank you for the clarification. I have tried both. support action doesn't work, but doesn't crash. – RedBassett Mar 27 '16 at 17:30
  • Sometime it's also needed to make that work, `getSupportActionBar().setHomeButtonEnabled(true);`. Try putting the above and this both statement. – Shree Krishna Mar 27 '16 at 17:32
  • Using both support action bar and action bar? Still crashes… What exactly are you saying I should try? – RedBassett Mar 27 '16 at 17:34
  • No, don't use `getActionBar()`. It is sure that you are using support library, So that I am saying you to put these both statement `getSupportActionBar().setDisplayHomeAsUpEnabled(true);` `getSupportActionBar().setHomeButtonEnabled(true);` – Shree Krishna Mar 27 '16 at 17:36
  • I get it. No, still doesn't go back to `MainActivity` and nothing shows in the console. – RedBassett Mar 27 '16 at 17:38
  • If that also didn't work then final option is to use the app icon to get back `getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_launcher);` – Shree Krishna Mar 27 '16 at 17:39
  • The app currently doesn't have an icon, but I switched it for a different drawable, and nothing. The button registers a press, but it goes nowhere. – RedBassett Mar 27 '16 at 17:41
  • I think you have cleared up the previous statement and put only `setHomeAsUpIndicator`. Don't remove them put all, and only after that try switching by removing unused one. – Shree Krishna Mar 27 '16 at 17:46
  • Nope, i tried different combinations of all the methods I've tried. Nothing changes regardless. – RedBassett Mar 27 '16 at 17:49
  • how you extending your class AppcompatActivity or AppCompatPreferenceActivity ??? – Sathish Kumar Mar 27 '16 at 18:42
  • `SettingsActivity` extends `AppCompatPreferenceActivity`, which was automatically generated by Android Studio's New > Settings Activity menu option. – RedBassett Mar 27 '16 at 18:47

1 Answers1

5

I suppose your code base was generated by Android Studio because I met the same problem. You can add the following code into your SettingsActivity.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    if (id == android.R.id.home) {
        finish(); // or go to another activity
        return true;
    }
    return super.onOptionsItemSelected(item);
}

This will make sure the up button works.

Will
  • 496
  • 8
  • 14