Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create BaseFragment and extend this Fragment in ChildFragment

I don't know whether this is repeated question or not, But I didn't found any solution.

The problem is, there is a screen which has similar views in all the screens, that screens are in fragment.

So, I want to create a base fragment and want to extend this base fragment in all child fragments.

I found on google, for demo examples, But I didn't find any solution.

I don't know from where to start.

Please help me.

I found this links but it is not much clear:

  1. Fragments inheritance Android

  2. When do I need a base activity and base fragment?

BaseFragment.java

public class BaseFragment extends Fragment {

  public BaseFragment() {
    // Required empty public constructor
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    return inflater.inflate(R.layout.fragment_base, container, false);
  }
}

ChildFragment.Java

public class ChildFragment extends BaseFragment{

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.layout.fragment_child, container, false);
  }
}

Now, there is a TextView in fragment_child layout. that is displayed when I run the app, but there are two other views in fragment_base, that is not showing...

enter image description here

like image 400
Mohd Asif Ahmed Avatar asked Sep 11 '17 07:09

Mohd Asif Ahmed


People also ask

Can fragment extends AppCompatActivity?

There are two ways to add a fragment to an activity: dynamically using Java and statically using XML. Before embedding a "support" fragment in an Activity make sure the Activity is changed to extend from FragmentActivity or AppCompatActivity which adds support for the fragment manager to all Android versions.

What is the fragment How fragments can be created?

A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment's view hierarchy becomes part of, or attaches to, the host's view hierarchy.


1 Answers

ABSTRACT CLASS APPROACH ?

I understand what you are trying to do (inheritance) here. But as pointed out by @hobokent, just by inheriting the BaseClass will not include your childView layout on top of BaseClass Layout. There are many ways to conquer this problem

Let's look into this solution.

  • Create an abstract class which will extend the Fragment Class (This will be BaseClass).

  • Make an abstract method which will return a layout.

  • Provide implementation for your abstract method in the ChildClass.

Here is the code snippet.

BaseClass

public abstract class BasicFragment extends Fragment {

  public void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
  }

  public View onCreateView(LayoutInflater inflater,ViewGroup parent, Bundle savedInstanseState)
   {
      View view = provideYourFragmentView(inflater,parent,savedInstanseState);
      return view;
   }

   public abstract View provideYourFragmentView(LayoutInflater inflater,ViewGroup parent, Bundle savedInstanceState);

}

ChildClass

public class ImageFragment extends BasicFragment{

  @Override
  public View provideYourFragmentView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.image_fragment,parent,false);

    //Now specific components here (you can initialize Buttons etc)

    return view;
   }


}

For your specific requirement, if you want the same layout to appear in child class layout. You can make a method which will return a layout and maybe in your child layout you make a placeholder for BaseClass Layout and add the BaseClass layout in childLayout using child.add(base_layout). Again it is just another design solution.
You can also put a common layout in Activity layout and Add fragment in Activity placeholder for the fragment. There are so many possible solutions.

I don't have any code specific to your requirement but here is the example where I have implemented this approach for TabLayout, where each tab is a different Fragment with a different layout.
Github full code sample.

like image 120
Rohit Singh Avatar answered Oct 13 '22 23:10

Rohit Singh