why fragment have default constructor?

0

Issue

there is default constructor in fragment, i want to know that what it’s use and what functionality it provides? and i run the code without it it worked perfectly and i can’t find any error in removing it

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);
      }
   }

Solution

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).

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).

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):

If a class contains no constructor declarations, then a default
constructor with no formal parameters and no throws clause is
implicitly declared.

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.

Answered By – James Fry

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More