Fragment强烈不推荐使用自定义带参的构造函数

Fragment 源码中有这样一段话

Default constructor. Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(android.os.Bundle) and later retrieved by the Fragment with getArguments().

Applications should generally not implement a constructor. The first place application code an run where the fragment is ready to be used is in onAttach(android.app.Activity), the point where the fragment is actually associated with its activity. Some applications may also want to implement onInflate(android.app.Activity,android.util.AttributeSet,android.os.Bundle) to retrieve attributes from a layout resource, though should take care here because this happens for the fragment is attached to its activity.

你可能感兴趣的:(Fragment强烈不推荐使用自定义带参的构造函数)