在我的情况下,我有一个使用 startActivityForResult 调用活动 B 的活动 A。
活动 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>