I'm using the recommended approach for Up Navigation and my code looks like this:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent h = new Intent(ShowDetailsActivity.this, MainActivity.class);
h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(h);
return true;
default: return super.onOptionsItemSelected(item);
}
}
Here's the use-case:
- I launch my app which is "MainActivity"
- I click a button to go to "ShowDetailsActivity"
- I click on the UP ActionBar navigation
The issue is after I click on UP, MainActivity hits its onCreate() methods all over again and loses all state instead of starting at the typical onResume() like it would if I just called "finish()" from ShowDetailsActivity. Why? Is this how it always works and this is expected behavior for Android to recreate all activities that are navigated to using the "Up" navigation approach? If I hit the back button I get the expected onResume lifecycle.
This is my solution if an Android "proper" ones doesn't exist:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent upIntent = new Intent(this, MainActivity.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
NavUtils.navigateUpTo(this, upIntent);
finish();
} else {
finish();
}
return true;
default: return super.onOptionsItemSelected(item);
}
}
Add the following to your parent activity in the manifest file
regarding to this answer
Beginning in Android 4.1 (API level 16), you can declare the logical parent of each activity by specifying the android:parentActivityName attribute in the element. If your app supports Android 4.0 and lower, include the Support Library with your app and add a element inside the . Then specify the parent activity as the value for android.support.PARENT_ACTIVITY, matching the android:parentActivityName attribute.
If the parent activity has launch mode , or the up intent contains FLAG_ACTIVITY_CLEAR_TOP, the parent activity is brought to the top of the stack, and receives the intent through its onNewIntent() method.
In Above Code "SingleTop" ,a new instance of a "singleTop" activity may also be created to handle a new intent. However, if the target task already has an existing instance of the activity at the top of its stack, that instance will receive the new intent (in an onNewIntent() call); a new instance is not created.
For Detail Documentation Click Here
To navigate up when the user presses the app icon, you can use the NavUtils class's static method, navigateUpFromSameTask(). For That Read The Documentaion given in the Link Above.
This code worked for me:
No finish() invocation required.
What works for me (and I also think the cleanest) is to reorder the activity to front. If it doesn't exist on the stack it will be created as you 'nav up'.
The reason, the activities are recreated when using up-navigation is, that android uses standard launch mode for this, if you do not specify an other mode. That means
and thus the activity is recreated (see docu here).
A solution would be to either declare the launch mode of the MainActivity as
in the
AndroidManifest.xml
(should always work together with
Intent.FLAG_ACTIVITY_CLEAR_TOP
)or you could add
FLAG_ACTIVITY_SINGLE_TOP
to your intent flags, to tell the activity, that it should not be recreated, if it is on the back stack, e.g.you just need to go back , not to create the activity again.