Paul
Paul

Reputation: 36349

Back button works, but the actionbar button does not?

I have a pretty simple use case which seems to be working for all my Activities except one. All my Activities are calling getActionBar().setDisplayHomeAsUpEnabled(true);. The only place this isn't working is on an Activity I just added, which is also the only one I'm launching from the main activity's info menu. The actual OS back button works as expected in this case, but the Home button is not. Here's the menu logic that starts the activity:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case R.id.share:
                share();
                return true;
            case R.id.support:
                emailSupport();
                return true;
            case R.id.settings:
                settings();
                return true;
            case android.R.id.home: 
                onBackPressed();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

The problem is with the SettingsActivity, called in the menu by the settings() method you see in the switch above:

private void settings(){
    Intent intent = new Intent(InfoMenuActivity.this, SettingsActivity.class);
    startActivity(intent);
}

And the onCreate of the SettingsActivity is pretty simple too:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);     
    prefs = this.getSharedPreferences(getString(R.string.preference_file), Context.MODE_PRIVATE);

    setView();
    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setDisplayShowHomeEnabled(true);
}

I originally only had the first getActionBar() call, but added the second to see if it changed anything, but it did not.

What am I missing?

Upvotes: 0

Views: 113

Answers (1)

Simas
Simas

Reputation: 44188

From the comments it seems that you don't know how to set the parent activity in the manifest and keep supporting older versions.

For API 16 you can use parentActivityName attribute but for older versions you'll have to add a meta tag:

<activity
    android:name="com.example.SettingsActivity"
    android:label="Settings Activity"
    android:parentActivityName="com.example.MainActivity" >
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.MainActivity" />
</activity>

If all you want to do is override the up button functionality to act as a back button then don't forget that in every activity you'll have to override onOptionsItemSelected.

However remember that overriding any default and expected android behaviour is bad practice.

Upvotes: 2

Related Questions