I have the below piece of code.
Fragment emptyViewFragment;
Fragment songListingFragment;
// .... and various codes ...
if (emptyViewFragment == null) {
emptyViewFragment =
(EmptyViewFragment) getFragmentManager().findFragmentByTag(EmptyViewFragment.TAG);
if (emptyViewFragment == null) {
emptyViewFragment = new EmptyViewFragment();
}
}
addFragment(emptyViewFragment, EmptyViewFragment.TAG);
if (songListingFragment == null) {
songListingFragment =
(SongListingFragment) getFragmentManager().findFragmentByTag(SongListingFragment.TAG);
if (songListingFragment == null) {
songListingFragment = new SongListingFragment();
}
}
addFragment(songListingFragment, SongListingFragment.TAG);
The two pieces of if-else codes looks very similar, and I'm thinking of making a function from it, so that I could just make my code as as below: -
EmptyViewFragment emptyViewFragment;
SongListingFragment songListingFragment;
// .... and various codes ...
createFragment(emptyViewFragment, EmptyViewFragment);
createFragment(songListingFragment, SongListingFragment);
or even better (since the 2nd parameter is the 1st parameter pass in object's type).
EmptyViewFragment emptyViewFragment;
SongListingFragment songListingFragment;
// .... and various codes ...
createFragment(emptyViewFragment);
createFragment(songListingFragment);
How should my createFragment function be written?