[英]Why do I sometimes get a stack trace when hiding a fragment?
I have a fragment that has inner fragments. 我有一个内部有碎片的碎片。
public void hideInnerFragment(String fr_tag) {
FragmentTransaction childTransaction = getChildFragmentManager().beginTransaction();
MyInnerFragment inner = (MyInnerFragment) getChildFragmentManager().findFragmentByTag(fr_tag);
if(inner != null) {
childTransaction.hide(inner);
childTransaction.commit();
}
}
Works fine. 工作正常。 But I have seen some times in the logs a stacktrace like the following: 但是我有时在日志中看到如下所示的堆栈跟踪:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1377)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1395)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:637)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:616)
at com.test.MyParentFragment.hideInnerFragment
Why do I get this one? 我为什么要得到这个? I haven't figured out when this happens. 我还不知道什么时候发生这种情况。
Update: 更新:
Just in case it helps/matters: 以防万一它有帮助/重要:
When the fragment is created I start an async task to do a long running operation and depending upon the result I call the hide of the inner fragment. 创建片段后,我将启动一个异步任务以执行长时间运行的操作,并根据结果将内部片段的隐藏称为“ hide”。
I am guessing you calling the method "hideInnerFragment" from an async task or something that is called after onPause() has been called. 我猜您是从异步任务或在调用onPause()之后调用的东西调用方法“ hideInnerFragment”。
I had a similar problem in my last app and I solved this by having a public static boolean in the hostactivity. 我在上一个应用程序中遇到了类似的问题,并通过在hostactivity中使用一个公共的静态布尔值来解决了这个问题。
public HostActivity extends Activity(){
public static boolean visible = false; // set it to false;
public void onResume(){
visible = true;
}
public void onPause(){
visible = false;
}
}
then in your fragment 然后在你的片段
public void hideInnerFragment(String fr_tag)
{
if(HostActivity.visible)
{
FragmentTransaction childTransaction = getChildFragmentManager().beginTransaction();
MyInnerFragment inner = (MyInnerFragment) getChildFragmentManager().findFragmentByTag(fr_tag);
if(inner != null) {
childTransaction.hide(inner);
childTransaction.commit();
}
}
If you are extending from FragmentActivity, then do all your operation related to fragment transaction inside onResumeFragments() method. 如果要从FragmentActivity扩展,则在onResumeFragments()方法内执行与片段事务相关的所有操作。
Alternatively You replace childTransaction.commit(); 或者,您替换childTransaction.commit(); with childTransaction. 与childTransaction。 commitAllowingStateLoss(). commitAllowingStateLoss()。
Alternatively you can override your onSaveInstance method and don't call super of it. 另外,您可以覆盖onSaveInstance方法,而不用对其进行超级调用。
All this is happening because support fragment had a bug which is already reported to Google code.google.com/p/android/issues/detail?id=23761 所有这一切都在发生,因为支持片段的错误已报告给Google code.google.com/p/android/issues/detail?id=23761
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.