Reputation: 663
Default Android Studio generated code:
public static Test newInstance(String param1, String param2) {
Test fragment = new Test();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
public Test() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
Is the null check really useful? In other word, will the argument bundle be destroyed in any case?
Edit: By the way, I cannot understand this either:
@Override
public void onDetach() {
super.onDetach();
mHandler = null;
}
The instance of the fragment is (or going to be) gone, so what is the meaning of setting mHandler to null?
Upvotes: 2
Views: 658
Reputation: 3346
If this fragment used by in a ViewPager and you are using Fragment"State"PagerAdapter, it should be needed.
Because FragmentStatePagerAdapter uses default constructor to create fragment when needed. In that case you are missing arguments.
In addition, If this fragment destroys for releasing memory and restores when needed, it would created with default constructor again. Same story here also.
Good luck.
Upvotes: 1