I'm new to Android programming and I'm studying some components and architecture.
I'm now testing Fragments.
I need to pass a List of custom objects from my Activity
to a nested Fragment
.
I read this question: How to pass a variable from Activity to Fragment, and pass it back? and I noticed that the Bundle
collection is the best practice to pass parameters during fragment creation.
Bundle bundle = new Bundle();
String myMessage = "Stackoverflow is cool!";
bundle.putString("message", myMessage );
FragmentClass fragInfo = new FragmentClass();
fragInfo.setArguments(bundle);
But I'm wondering why can't I simply pass my parameters via constructor, say:
List<CustomObject> myParameter = new ArrayList<CustomObject>();
MyFragment fragInfo = new MyFragment(myParameter); // custom Fragment constructor
Is this a wrong approach? Is the empty constructor used by the framework to instantiate the fragment via reflection or something similar?