替换 SingleTask 活动中的片段

Replacing fragments in SingleTask activities

Activity A 是一个 SingleTask activity。在其 OnCreate 回调中,我创建了一个片段并将其附加到布局。

Activity B 现在启动,A 暂停并调用其 OnSaveInstanceState。

Activity 现在使用新意图重新调用 A(由于 B 中的交互)。在 OnNewIntent() 中,我根据新意图创建了一个新片段并替换了之前的片段。

这会导致 IllegalStateException:无法在 onSaveInstanceState 之后执行此操作。

正确的做法是什么?即使调用了 OnNewIntent(),框架仍然认为 A 已暂停。

您需要调用 super.onNewIntent(intent) 来更新 FragmentManager 的状态,以便您可以执行 FragmentTransactions。