【问题标题】:FragmentTransaction add fragment after replace old fragment but onDestroyView calledFragmentTransaction 在替换旧片段后添加片段但调用 onDestroyView
【发布时间】:2020-06-21 12:27:48
【问题描述】:

我替换了 FragmentA :

 FragmentManager fm = MainActivity.getGlobal().getSupportFragmentManager();
 FragmentTransaction ft = fm.beginTransaction();
 ft.addToBackStack(fragmentA.getClass().getSimpleName());
 ft.replace(relId, fragmentA,fragmentA.getClass().getSimpleName());
 ft.commitAllowingStateLoss();

并添加 FragmentB :

FragmentManager fm = MainActivity.getGlobal().getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(relId, fragmentB);
ft.commitAllowingStateLoss();

添加FragmentA:onDestroyView后调用,如何解决?我希望 FragmentB 添加和 Fragment Not DestroyView

谢谢

【问题讨论】:

    标签: android android-fragments fragmentmanager fragmenttransaction


    【解决方案1】:

    对replace() 的调用类似于在A 上调用remove() 而他们在B 上调用add() 因此当您替换它时无法保存A,它是onDestroy()、onDestroyView() 等将是调用。

    但您可以调用 add() 来代替调用 replace() 为 B 在 A 顶部添加 B 而无需删除 A 并将 B 添加到后堆栈以在后按时导航到 A

    祝你有美好的一天,阿里

    【讨论】:

    • 使用 add 而不是 replace 它会将片段 B 添加到 A 之上,而不是将 A 替换为 B
    猜你喜欢
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 2022-12-15
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多