【问题标题】:TaskStackBuilder with startActivityForResult带有 startActivityForResult 的 TaskStackBuilder
【发布时间】:2016-08-22 08:53:23
【问题描述】:

在我的例子中,我有一个活动 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);

最后,我在 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>

【问题讨论】:

  • 您找到了完美的方法吗?我也有同样的场景
  • 一点也不!只是解决方法,但这不是一件容易的事。对不起!

标签: android taskstackbuilder


【解决方案1】:

根据我对Android框架的了解,OnActivityResult方法只有在一个activity通过startActivityForResult()方法启动并且相应的activity调用了setResult时才会被调用。

Android官方文档Android TaskStackBuilder

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

所以我认为您不能对框架说使用该回调返回活动。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-19
    • 2017-01-04
    • 1970-01-01
    • 2022-12-29
    • 2016-02-26
    相关资源
    最近更新 更多