为什么片段具有默认构造函数? [英] why fragment have default constructor?
问题描述
片段中有默认构造函数,我想知道它的用途以及它提供的功能吗?并且我在没有代码的情况下运行代码,并且在删除代码时找不到任何错误
public class SongListFragment extends Fragment {
private static final String SONG_IDS = "song_ids";
// TODO: Rename and change types of parameters
private int[] songIds;
private OnFragmentInteractionListener mListener;
public SongListFragment() {
// Required empty public constructor
}
// TODO: Rename and change types and number of parameters
public static SongListFragment newInstance(int[] songIds) {
SongListFragment fragment = new SongListFragment();
Bundle args = new Bundle();
args.putIntArray(SONG_IDS, songIds);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
songIds = getArguments().getIntArray(SONG_IDS);
}
}
@Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState )
{
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_song_list, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onSongSelected(10);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
}
else {
throw new RuntimeException( context.toString() +
" must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
public void onSongSelected(int songId);
}
}
推荐答案
请参阅此问题和评论/答案.简而言之,Fragments需要为Android系统使用no-args构造函数来实例化它们(我相信活动历史记录管理器会执行此操作,等等).
See this question and comments / answers. In short, Fragments need to have a no-args constructor for the Android system to instantiate them (I believe the activity history manager does this, etc).
如果构造函数是显式的(如未更改的示例中所示),则确实可以确保在添加其他构造函数的情况下no-args构造函数可以正常工作,并且注释起着提醒作用(即,或原始作者并未真正了解该语言的目的和/或工作方式.
If the constructor is explicit, as in the unaltered example, then it's really there to ensure the no-args constructor works if other constructors are added, and the comment serves as a reminder (that or the original author didn't really understand the purpose and/or how the language works).
如果no-args构造函数可能是隐式的-即在源代码中省略了它,并且没有声明其他构造函数-则将根据 JLS (这是在示例中删除构造函数时发生的情况):
If the no-args constructor may be implicit - ie it is omitted in the source and there are no other constructors declared - then one is created behind the scenes as per the JLS (this is what happened when you deleted the constructor in your example):
如果一个类不包含构造函数声明,则使用默认值没有正式参数且没有throws子句的构造函数是隐式声明.
If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.
如果要声明的类是原始类Object,则默认构造函数的主体为空.否则,默认构造函数只调用no的超类构造函数争论.
If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.
这篇关于为什么片段具有默认构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!