Reputation:
I have 5 fragments, where each fragment class has the exact same method:
public class FirstFragment extends Fragment {
// other methods
public void clear() {
data.clear();
adapter.notifyDataSetChanged();
}
}
The second fragment:
public class SecondFragment extends Fragment {
// other methods
public void clear() {
data.clear();
adapter.notifyDataSetChanged();
}
}
And so on...
How can I organize my code better so that I don't have to copy and paste this method into each of my 5 fragments (i.e. make one method, call the same method in each fragment).
Upvotes: 1
Views: 1139
Reputation: 1235
you need to use the concept of Inheritance, put all the common variables and methods in a BaseFragment
and extend others from it.
public class BaseFragment extends Fragment {
public Data data;
public Adapter adapter;
public void clear() {
data.clear();
adapter.notifyDataSetChanged();
}
}
First:
public class FirstFragment extends BaseFragment {
// other methods
}
Second:
public class SecondFragment extends BaseFragment {
// other methods
}
Upvotes: 5