commitAllowingStateLoss()的片段活动 [英] commitAllowingStateLoss() in fragment activities

查看:442
本文介绍了commitAllowingStateLoss()的片段活动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用片段的活动,它是在唯一肖像模式,并且没有办法旋转屏幕。

my app uses fragment activities, it is in portrait mode only and there is no way to rotate the screen.

我原来用的是提交()的方法,但现在我打算胡乱这些更改为 commitAllowingStateLoss()对于片段活动

Originally I was using the commit() method but now I plan to indiscriminately change these to commitAllowingStateLoss() for the fragment activities

有什么理由不加选择地做到这一点,而无需重新评估,我用一个片段逐案?

Is there any reason not to indiscriminately do this without re-evaluating each individual case where I use a fragment?

推荐答案

如果我理解正确的话,你的意思是:有什么理由不加选择地做到这一点,而无需重新评估,我用一个片段逐案

If I understand correctly you mean : Is there any reason NOT to indiscriminately do this without re-evaluating each individual case where I use a fragment?

答案是肯定的 - 你不应该没有仔细重新评估,你用一个片段每个案件做到这一点。

The answer is Yes - you should not do this without carefully re-evaluating each individual case where you use a fragment.

当然,通过因配置的变化(屏幕旋转)你排除问题的关键领域之一preventing重新启动:即用户可以旋转​​屏幕调用的onSaveInstanceState commitAllowingStateLoss BEFORE 。在这种情况下,用户界面​​的一个片段或部分可能会丢失。对于这样的一个非正式的讨论,请参阅该<一href="http://source.9nit.com/android/any-side-effect-to-solve-ldquo-can-not-perform-this-action-after-onsaveinstancestate-rdquo-by-using-commitallowingstateloss.html-72805.html">post.

Of course, by preventing restarts due to config changes (screen rotations) you have eliminated one of the key problem areas : i.e. the user could rotate the screen AFTER a call to onSaveInstanceState but BEFORE the commitAllowingStateLoss. In this case a fragment or portion of UI might be lost. For an informal discussion of this, see this post.

但也有你应该考虑其他的情况下,更换前提交 commitAllowingStateLoss

But there are other situations you should consider before replacing commit by commitAllowingStateLoss.

  1. 基本上的onSaveInstanceState和commitAllowingStateLoss之间的任何用户界面的更新: <一href="http://stackoverflow.com/questions/3353023/android-illegalstateexception-when-is-it-thrown">Android: IllegalStateException异常 - 当它抛出

  1. Basically, any UI updates between onSaveInstanceState and the commitAllowingStateLoss: Android: IllegalStateException - When is it thrown?

如果您有任何的无头片段更新活动的用户界面,然后他们的一些更新可能会丢失(见本的文章)。

If you have any headless fragments that update the UI of your activity then some of their updates might be lost (see this article).

Android的可能杀的片段,因为手机/标签运行资源不足(见本<一href="http://stackoverflow.com/questions/7536988/android-app-out-of-memory-issues-tried-everything-and-still-at-a-loss">answer).

Android might "kill" a fragment because the phone/tab is running low on resources (see this answer).

当然,如果屏幕旋转是pvented $ P $,然后的onSaveInstanceState 可能不会被调用,在这种情况下丢失机会的更新窗口增加

Of course, if screen rotations are prevented, then onSaveInstanceState may not be called, in which case the window of opportunity for an update to be lost is increased.

如果你决定使用 commitAllowingStateLoss 再有事情可以做,以尽量减少所涉及的风险:例如:考虑做一个提交 / executePendingTransactions 当父活动在下次重新启动(我知道你不希望这样做,但其他人可能会读<一href="http://stackoverflow.com/questions/7998507/how-do-you-handle-fragment-transactions-when-the-state-of-the-parent-activity-is">this).

If you do decide to use commitAllowingStateLoss then are things you can do to minimize the risks involved: e.g. consider doing a commit / executePendingTransactions when the parent activity is next restarted (I know you don't want to do this, but someone else might read this).

最后(也是以防别​​人读这 - 这不是有关你的情况),有处理的 IllegalStateException异常比承诺转移到<$可能更安全的方法C $ C> commitAllowStateLoss 。例如,你可以只坚持提交和办理 IllegalStateException异常。或者,你可能已经打了Android的一个错误并有可能是一个解决方法。

Finally (again in case someone else reads this - this is not relevant in your case) there are probably safer ways of handling an IllegalStateException than moving from commit to commitAllowStateLoss. e.g you could just stick with commit and handle the IllegalStateException. Alternatively, you may have hit a bug in Android and there might be a workaround.

这篇关于commitAllowingStateLoss()的片段活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆