带有startActivityForResult的TaskStackBuilder

fol*_*olo 6 android taskstackbuilder

在我的例子中,我有一个活动A,它使用startActivityForResult调用活动B.

活动B是将数据返回到活动A的表单,因此数据可以存储在我的数据库中.

此外,我的应用程序启动通知,单击时启动活动B,当我尝试从活动B返回到活动A时出现问题,因为从未调用方法"onActivityResult".在创建TaskStackBuilder时,我无法模拟startActivityForResult():

Intent resultIntent = new Intent(this, activityB.class);
// This ensures that navigating backward from the Activity leads out of your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(activityB.class);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);

NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                .setContentIntent(resultPendingIntent);
Run Code Online (Sandbox Code Playgroud)

最后,我在manifest.xml中添加了活动B的父活动:

<activity
    android:name=".activityB"
    android:parentActivityName=".activityA"
    android:windowSoftInputMode="stateHidden">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value=".activityA"/>
</activity>
Run Code Online (Sandbox Code Playgroud)

小智 3

据我对 Android 框架的了解,仅当使用 startActivityForResult() 方法启动活动并且相应活动调用了 setResult 方法时,才会调用 OnActivityResult 方法。

Android 官方文档Android TaskStackBuilder:

用于构建跨任务导航的合成返回堆栈的实用程序类

因此,我认为您不能让框架使用该回调返回到活动。

相反,您可以做的是在 Intent 中添加一些额外的内容,然后当您返回到返回堆栈(到活动 A)时,检查初始方法(onCreate 或 onResume)中是否存在仅在以下情况下才存在的额外内容或参数:来自最后一项活动(活动 B)