Yes, it is allowed. Why not? For example, if you have several fragments that display lists, you can put all common methods in a FragmentList and then inherit other fragments, adding only unique methods or redefining them from super, if necessary.
But overriding onCreateView() can cause difficulties in handling layouts. In my last project, I instead created the inflateFragment() method in a superclass as follows:
Basefragment.java
protected View inflateFragment(int resId, LayoutInflater inflater, ViewGroup container) { View view = inflater.inflate(resId, container, false); FrameLayout layout = (FrameLayout)view.findViewById(R.id.fragment_layout); . . . setHasOptionsMenu(true); return view; }
Due to the structure, each fragment layout resource is wrapped in a FrameLayout using id = fragment_layout . But you can use LinearLayout or another parent view you need.
And then in the inherited fragments:
@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflateFragment(R.layout.my_fragment, inflater, container); ... return view; }
source share