gpt4 book ai didi

android - onActivityResult() fragment 事务错误

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:21 28 4
gpt4 key购买 nike

我正在尝试替换 onActivityResult() 中的 fragment 但它总是给我 java.lang.illegalStateException 错误。

Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1377)
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime: at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1395)
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime: at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime: at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
12-29 18:50:21.455 5554-5554/com.package E/AndroidRuntime: at .virtualClass.VirtualPurchaseFragment.dealWithSuccessfulPurchase(VirtualPurchaseFragment.java:161)

这是我的onActivityResult代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

Fragment fragment = new VirtualListFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();

}

最佳答案

终于找到解决办法了,fragment cannot be swaped or replaceed in onActivityResult()

我们需要把这段代码放在Handler或者OnResume()

解决方案:

private boolean change_fragment=false;

OnActivityResult()代码

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

change_fragment=true;
}

OnResume() 代码

@Override
public void onResume() {
super.onResume();
if(change_fragment)
{
change_fragment=false;
Fragment fragment = new VirtualListFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
}
}

关于android - onActivityResult() fragment 事务错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34512011/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com