Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigate back from settings activity

Tags:

I'm playing around with Android Studio so I created a SettingsActivity using the wizard and I'm faced with the problem that it is not possible to navigate from this settings activity back to the main activity using the "up" arrow in the Actionbar.

The setup of the Actionbar looks like this:

private void setupActionBar() {
    ActionBar actionBar = getSupportActionBar();
    if (actionBar != null) {
        // Show the Up button in the action bar.
        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

Actionbar is not null btw.
And the parentActitvityName is set in the AndroidManifest:

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

However, a click on the arrow does nothing. Not even onOptionsItemSelected gets triggered.

Seems like this is exactly the same problem Action bar setDisplayHomeAsUpEnabled not working on ICS but navigating back from a detail to an overview activity is working fine in the very same app. Moreover I set MinSDK to 15 and TargetSDK to 23.

like image 936
Daniel Avatar asked Dec 11 '15 11:12

Daniel


1 Answers

override the onOptionsItemSelected method on your AppCompatPrefernceActivity and make it like this

   @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            // Respond to the action bar's Up/Home button
            case android.R.id.home:
                super.onBackPressed();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
like image 146
Vasileios Pallas Avatar answered Oct 02 '22 12:10

Vasileios Pallas