So at the moment I have an activity that can be reached from two different activities, the problem is that I can only set one activity as the parent activity in the manifest XML file. Obviously this is bad UX/UI design because the activity may send the user back to the wrong activity they were at previously and so I'm trying to dynamically set which activity is the parent activity.
The trouble is I'm not quite sure how to go about this, whether in code or XML so any pointers are appreciated. :)
Declare a Parent Activity 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). To support devices with older versions of Android, define a <meta-data> name-value pair, where the name is "android.
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
The Up button is used to navigate within an app based on the hierarchical relationships between screens. For instance, if screen A displays a list of items, and selecting an item leads to screen B (which presents that item in more detail), then screen B should offer an Up button that returns to screen A.
For future readers here's an example of how to actually implement the official/proper solution as per the developer guides (scroll to the paragraph beginning with "This is appropriate when the parent activity may be different...").
Note that this solution assumes you are using the Support Library to implement your ActionBar
and that you can at least set a 'default' parent Activity in your manifest XML file to fallback on if the Activity you are backing out of is in a 'task' that doesn't belong to your app (read the linked docs 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 (i.e. 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 a default parent Activity in the manifest XML file then you'll also need to implement onCreateSupportNavigateUpTaskStack() since the system will have no idea how to generate a backstack for your task. I have not provided any example for this part.
finish()
type solutionsOn my searching for a solution to this problem I came across a few answers that advocate the strategy of overriding onOptionsItemSelected()
and intercepting the android.R.id.home
button so they could simply finish()
the current Activity
to return to the previous screen.
In many cases this will achieve the desired behavior, but I just want to point out that this is definitely not the same as a proper UP navigation. If you were navigating to the child Activity through one of the parent Activities, then yes finish()
will return you to the proper previous screen, but what if you entered the child Activity through a notification? In that case finish()
ing by hitting the UP button would drop you right back onto the home screen or whatever app you were viewing before you hit the notification, when instead it should have sent you to a proper parent Activity within your app.
Like this way you can navigate dynamically to your parent activity:
getActionBar().setDisplayHomeAsUpEnabled(true); @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // Respond to the action bar's Up/Home button case android.R.id.home: finish(); return true; } return super.onOptionsItemSelected(item); }
NOTE: It redirects you to the activity or fragment where you came from, no matter whether it's a parent or not. Clicking on the action bar Up/Home button will just finish the current activity.
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