Fragment.newInstance()VC的onSaveInstanceState() [英] Fragment.newInstance() vc onSaveInstanceState()

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

问题描述

为什么推荐(不同来源)不超载的构造片段取值,但使用静态 Fragment.newInstance()与传递捆绑来呢?

当你重载一个构造函数,你只需明确定义默认之一。比,如果你的片段将被重新出于某种原因,你用的onSaveInstanceState()随后的数据中提取的的onCreate()。使用类似的情况 Fragment.newInstance(),你并不需要创建公共默认构造函数的唯一区别。

我是不是认识什么了吗?非常感谢你。

解决方案
  

为什么推荐(不同来源)不超载的构造器碎片,但使用静态Fragment.newInstance()与传递一个包呢?

Android的自动重新创建一个配置更改所有非保留的片段(如屏幕旋转),并且将使用零参数构造的。该捆绑通过提供setArguments()另存为实例状态的一部分,并给予新重建片段。因此,你只需要实现一种方法(工厂方法),而不是三(非零参数构造函数的的onSaveInstanceState() onViewStateRestored())拍摄您建议的方法。

  

我是理解什么不对?

如果你的作品,去了。当你注意,工厂方法的方法是建议,不是必须的。

Why is it recommended (different sources) not to overload the constructor for Fragments but use static Fragment.newInstance() with passing a Bundle to it?

When you overload a constructor you just explicitly define default one. Than, if your Fragment would be recreated for some reason you use onSaveInstanceState() with subsequent data extracting on onCreate(). The similar situation with using Fragment.newInstance(), the only difference you don't need to create public default constructor.

Am I understanding something wrong? Thank you very much.

解决方案

Why is it recommended (different sources) not to overload the constructor for Fragments but use static Fragment.newInstance() with passing a Bundle to it?

Android automatically recreates all non-retained fragments on a configuration change (e.g., screen rotation), and it will use the zero-argument constructor for that. The Bundle supplied via setArguments() is saved as part of the instance state and given to the newly-recreated fragment. Hence, you only have to implement one method (the factory method) as opposed to three (a non-zero-arguments constructor and onSaveInstanceState() and onViewStateRestored()) to take the approach that you suggest.

Am I understanding something wrong?

If it works for you, go for it. As you note, the factory method approach is a recommendation, not a requirement.

这篇关于Fragment.newInstance()VC的onSaveInstanceState()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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