parent activity with implicit intents

2019-06-22 08:29发布

问题:

I am successfully using the attribute android:parentActivityName in manifest, in order to set the parent activity (activity A) when another activity (activity B) is started by a push notification, for example. Then, if I go back, I navigate to activity A.

However, it doesn't work with implicit intents. I have an intent-filter declared in manifest for activity B. When activity B is launched from outside the app, it does not seem to effect the attribute android:parentActivityName (or meta-data android.support.PARENT_ACTIVITY with lower APIs).

How I can set the parent activity in that case?

The block of manifest:

<activity
        android:name="com.domain.app.activities.ActivityB"
        android:screenOrientation="portrait"
        android:windowSoftInputMode="stateUnchanged"
        android:parentActivityName="com.domain.app.activities.ActivityA" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.domain.app.activities.ActivityA" />
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT" />
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="http" android:host="domain.com" android:pathPattern=".*" />
        </intent-filter>
    </activity>

Any help is appreciated.

Thanks!

回答1:

From developers documentation: https://developer.android.com/training/implementing-navigation/ancestral

Navigate up with a new back stack If your activity provides any intent filters that allow other apps to start the activity, you should implement the onOptionsItemSelected() callback such that if the user presses the Up button after entering your activity from another app's task, your app starts a new task with the appropriate back stack before navigating up.

You can do so by first calling shouldUpRecreateTask() to check whether the current activity instance exists in a different app's task. If it returns true, then build a new task with TaskStackBuilder. Otherwise, you can use the navigateUpFromSameTask() method as shown above.

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        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.
            NavUtils.navigateUpTo(this, upIntent);
        }
        return true;
    }
    return super.onOptionsItemSelected(item);
}

Note: In order for the addNextIntentWithParentStack() method to work, you must declare the logical parent of each activity in your manifest file, using the android:parentActivityName attribute (and corresponding element) as described above.



回答2:

As mentioned in Developer site "specifying your parent activity functionality" is provided in API level 16.

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.

and its also state there that for lower api's android provides support library.

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.

here is the link : http://developer.android.com/training/implementing-navigation/ancestral.html#SpecifyParent

and you can also see this post.