简体   繁体   English

更快的片段屏幕方向变化

[英]Faster screen orientation change for fragments

Android activities have a pair of methods onRetainNonConfigurationInstance()/getLastNonConfigurationInstance() for keeping objects expensive for load during configuration changes. Android 活动有一对 onRetainNonConfigurationInstance()/getLastNonConfigurationInstance() 方法,用于在配置更改期间保持对象的负载昂贵。

What should be a nice behavior for fragments in such case?在这种情况下,片段的良好行为应该是什么? Is setRetainInstance() a solution? setRetainInstance() 是一个解决方案吗? What about usage of configuration-specific resources after setting it to true?将其设置为 true 后,配置特定资源的使用情况如何?

Thank you.谢谢你。

Do you really have to support orientation changes?你真的必须支持方向变化吗?

If not, you can override this behavior in your AndroidManifest-file.如果没有,您可以在 AndroidManifest 文件中覆盖此行为。

If you need to support orientation change, say because you need a different layout, then I have found setRetainInstance works for me making use of onSaveInstanceState if need be, using the saved state info in onCreateView or onActivityCreated .如果您需要支持方向更改,例如因为您需要不同的布局,那么我发现setRetainInstance对我有用,如果需要,可以使用onSaveInstanceState ,使用在onCreateViewonActivityCreated中保存的 state 信息。

With regard configuration specific resources, these won't be affected by setting setRetainInstance you'll still get access to the correct resources for your orientation etc.in onCreateView and so forth after an orientation change.关于配置特定的资源,这些不会受到设置setRetainInstance的影响,在方向更改后,您仍然可以在onCreateView等中访问正确的资源以用于您的方向等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM