Reputation: 12783
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:
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);
}
}
Upvotes: 74
Views: 20159
Reputation: 747
This code worked for me:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
// This activity is NOT part of this app's task, so create a new task
// when navigating up, with a synthesized back stack.
TaskStackBuilder.create(this)
// Add all of this activity's parents to the back stack
.addNextIntentWithParentStack(upIntent)
// Navigate up to the closest parent
.startActivities();
} else {
// This activity is part of this app's task, so simply
// navigate up to the logical parent activity.
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
NavUtils.navigateUpTo(this, upIntent);
}
return true;
No finish() invocation required.
Upvotes: 12
Reputation: 5566
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
"The system always creates a new instance of the activity in the target task"
and thus the activity is recreated (see docu here).
A solution would be to either declare the launch mode of the MainActivity as
android:launchMode="singleTop"
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.
Intent h = NavUtils.getParentActivityIntent(this);
h.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
NavUtils.navigateUpTo(this, h);
Upvotes: 63
Reputation: 3691
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.
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
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.
<activity
android:launchMode="singleTop"
android:name="com.example.myfirstapp.MainActivity">
</activity>
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.
Upvotes: 4
Reputation: 1075
you just need to go back , not to create the activity again.
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
super.onBackPressed();
return true;
}
return super.onOptionsItemSelected(item);
}
Upvotes: 1
Reputation: 5413
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'.
Intent upIntent = NavUtils.getParentActivityIntent(this);
upIntent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(upIntent);
finish();
return true;
Upvotes: 1