Reputation: 4748
I found myself writing this part of code over and over again in all of the fragments:
public class Tab1 extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle bundle = this.getArguments();
if (bundle == null) {
return null;
}
@SuppressWarnings("unchecked")
List<Map<String, String>> data = (List<Map<String, String>>) bundle.getSerializable("data");
/ ........../
}
}
I'm not sure how to refactor it into a class to avoid repetition. I'm getting an error passing the reference of a class (It could be Tab1 or Tab2 or Tab3)into it:
public class GetBundle {
public <T> T serialize(Class clazz,String key){
Bundle bundle = clazz.getArguments();
^^^^
if (bundle == null) {
return null;
}
@SuppressWarnings("unchecked")
T data = (T) bundle.getSerializable(key);
return data;
}
}
Upvotes: 0
Views: 50
Reputation: 8578
In your serialize
method,Class
type doesn't have method getArguments
,so you should change clazz
type to Fragment
.
Like this:
public <T> T serialize(Fragment clazz,String key){
Upvotes: 1