0

In my android app i am using up navigation icon on actionbar. In my child activity i have set

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);

In manifest I have set

<activity
        android:name="app.sclms.UserAccount"
        android:label="@string/app_name"
        android:parentActivityName="app.sclms.MainMenu">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="app.sclms.MainMenu" />

</activity>

But whenever I click on up icon my application get exited.

What is missing here, I don't understand.

Alexander Farber
  • 21,519
  • 75
  • 241
  • 416
Amol
  • 3
  • 2

2 Answers2

1

Please read this section: http://developer.android.com/training/implementing-navigation/ancestral.html#NavigateUp

Specifically, add this code:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}
Omar
  • 7,835
  • 14
  • 62
  • 108
-1

I however do not recommend using navigateUpFromSameTask as it will cause your ParentActivity to be re-created instead of being resumed on ICS and lower. The reason for this behavior is because NavUtils behaves differently for Pre JellyBean and Post JellyBean as explained in this SO.

A better way is to do this is to handle the Up action manually in the Child Activity:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            startActivity(new Intent(this, ParentActivity.class)
                .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP));
        default:
            return super.onOptionsItemSelected(item);
    }
}

And best part of this manual approach is that it works as expected(resumes the ParentActivity instead of ReCreating it) for all API Levels.

Community
  • 1
  • 1
kpsfoo
  • 392
  • 4
  • 18