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

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

问题描述

我的应用使用 Fragment Activity,它仅处于纵向模式,无法旋转屏幕.

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

最初我使用的是 commit() 方法,但现在我计划不加选择地将它们更改为 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.

当然,通过防止由于配置更改(屏幕旋转)而重新启动,您已经消除了一个关键问题区域:即用户可以在调用 onSaveInstanceState 之后旋转屏幕,但在 <代码>commitAllowingStateLoss.在这种情况下,UI 的一个片段或部分可能会丢失.有关此问题的非正式讨论,请参阅此 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.

但是在将 commit 替换为 commitAllowingStateLoss 之前,您应该考虑其他情况.

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

  1. 基本上,onSaveInstanceState 和 commitAllowingStateLoss 之间的任何 UI 更新:Android:IllegalStateException - 何时抛出?

如果您有任何更新活动 UI 的无头片段,那么它们的某些更新可能会丢失(请参阅此 文章).

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 可能会杀死"一个片段,因为手机/标签页资源不足(请参阅此 答案).

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

当然,如果屏幕旋转被阻止,那么 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 那么您可以采取哪些措施来最大程度地降低所涉及的风险:例如考虑在下次重新启动父活动时执行 commit/executePendingTransactions(我知道您不想这样做,但其他人可能会阅读 这个).

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).

最后(再次,以防其他人读到这个 ​​- 这与您的情况无关)可能有比从提交移动到 commitAllowStateLoss 更安全的处理 IllegalStateException 的方法.例如,您可以坚持提交并处理 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天全站免登陆