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); } }
Declare a Parent Activity To support the up functionality in an activity, you need to declare the activity's parent. You can do this in the app manifest, by setting an android:parentActivityName attribute. The android:parentActivityName attribute was introduced in Android 4.1 (API level 16).
Explanation. Generally, every activity is having its UI(Layout). But if a developer wants to create an activity without UI, he can do it.
On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.
Add the following to your parent activity in the manifest file
android:launchMode="singleTop"
regarding to this answer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With