For future readers, here is an example of how to really implement the official / correct decision in accordance with the developer's guides (scroll to the point starting with "This is suitable when parenting activity can be different ... ").
Please note that this solution assumes that you are using the Support Library to implement your ActionBar and that you can at least set the default parent activity in your manifest XML file for backup if the action you perform is in the "task" that does not belong to your application (read the related documents for clarification).
// Override BOTH getSupportParentActivityIntent() AND getParentActivityIntent() because // if your device is running on API 11+ it will call the native // getParentActivityIntent() method instead of the support version. // The docs do **NOT** make this part clear and it is important! @Override public Intent getSupportParentActivityIntent() { return getParentActivityIntentImpl(); } @Override public Intent getParentActivityIntent() { return getParentActivityIntentImpl(); } private Intent getParentActivityIntentImpl() { Intent i = null; // Here you need to do some logic to determine from which Activity you came. // example: you could pass a variable through your Intent extras and check that. if (parentIsActivityA) { i = new Intent(this, ActivityA.class); // set any flags or extras that you need. // If you are reusing the previous Activity (ie bringing it to the top // without re-creating a new instance) set these flags: i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); // if you are re-using the parent Activity you may not need to set any extras i.putExtra("someExtra", "whateverYouNeed"); } else { i = new Intent(this, ActivityB.class); // same comments as above i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); i.putExtra("someExtra", "whateverYouNeed"); } return i; }
NOTE. If you do not set the default parental activity in the XML manifest file, you will also need to implement onCreateSupportNavigateUpTaskStack () , since the system will not have the idea of ββcreating a freeze frame for your task. I have not provided a single example for this part.
My thoughts on solutions like finish()
Looking for a solution to this problem, I came across several answers that advocate a strategy for overriding onOptionsItemSelected() and intercepting the android.R.id.home button so that they can simply finish() current Activity back to the previous screen.
In many cases, this will lead to the desired behavior, but I just want to point out that this is definitely not the same as the correct rewind. If you switched to a child activity through one of the parent actions, then yes finish() will return you to the previous previous screen, but what if you entered the child activity through a notification? In this case, finish() ing by pressing the UP button, you will be taken back to the main screen or to any other application that you viewed before you clicked the notification, instead it should send you to the appropriate parental activity in your application.
Tony Chan Feb 05 2018-01-15T00: 00Z
source share