Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically set parent activity

Tags:

android

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. :)

like image 356
Darryl Bayliss Avatar asked Oct 04 '13 14:10

Darryl Bayliss


People also ask

How do I set parent activity?

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.

How to attach Fragment to activity?

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.

What is Up button Android?

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.


2 Answers

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.

My thoughts on the finish() type solutions

On 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 image 76
Tony Chan Avatar answered Oct 03 '22 02:10

Tony Chan


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.

like image 24
Saj Avatar answered Oct 03 '22 03:10

Saj