具有隐式意图的父活动 [英] parent activity with implicit intents

查看:89
本文介绍了具有隐式意图的父活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,我成功使用清单中的android:parentActivityName属性,以便在通过推送通知启动另一个活动(活动B)时设置父活动(活动A).然后,如果我返回,则导航到活动A.

但是,它不适用于隐式意图.我在清单B中为活动B声明了一个意图过滤器.从应用程序外部启动活动B时,它似乎不会影响属性android:parentActivityName(或使用较低API的元数据android.support.PARENT_ACTIVITY)./p>

在这种情况下如何设置父级活动?

清单清单

<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>

感谢您的帮助.

谢谢!

解决方案

摘自开发人员文档: https://developer.android.com/training/implementing-navigation/ancestral

使用新的后堆栈进行导航 如果您的活动提供了任何意图过滤器,从而允许其他应用开始活动,您应该实现 onOptionsItemSelected ()回调,这样,如果用户在从另一个应用程序的任务输入您的活动后按下向上"按钮,则您的应用程序会在向上导航之前使用适当的后台堆栈启动一个新任务.

您可以先调用 TaskStackBuilder 构建新任务.否则,您可以使用 navigateUpFromSameTask()方法,如上所示.

@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);
}

注意:为了使 addNextIntentWithParentStack()方法正常工作,您必须使用 android声明清单文件中每个活动的逻辑父级如上所述的:parentActivityName 属性(和相应的元素).

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!

解决方案

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.

这篇关于具有隐式意图的父活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆