我可以从 Fragment Constructor 调用 setHasOptionsMenu() 吗?

Can I call setHasOptionsMenu() from Fragment Constructor?

从构造函数中调用 setHasOptionsMenu(true),这显然在 onCreate() 之前被调用,效果很好!我可以这样做吗?会有什么问题?

是的,您可以从构造函数中调用 setHasOptionsMenu(true)

点这里fragment - Android

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

您可能需要为此使用另一个生命周期事件。