0

I have an SchoolActivity that has two buttons: -Primary (adds the PrimaryFragment) -Secondary (adds the SecondaryFragment)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.yuv.mycollege.MainActivity">
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

    <Button
        android:id="@+id/button_primary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Primary"/>

    <Button
        android:id="@+id/button_secondary"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Secondary"/>
    </LinearLayout>

    <!-- Main content area for fragments-->
    <FrameLayout
        android:background="@color/colorPrimary"
        android:id="@+id/main_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:padding="4dp"/>

</LinearLayout>

Both the fragments has content and footer area, which are themselves are fragments (PrimaryContentFragment, PrimaryFooterFragment, SecondaryContentFragment, SecondaryFooterFragment)

I am adding the fragments from the activity using:

public void onClick(View view) {
    Button button = (Button)view;
    Toast.makeText(this, "Going to Add Children", Toast.LENGTH_SHORT).show();

    switch(view.getId()) {
        case R.id.button_primary:
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.main_container, new PrimaryFragment())
                    .addToBackStack("primary")
                    .commit();
            break;
        case R.id.button_secondary:
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.main_container, new SecondaryFragment())
                    .addToBackStack("secondary")
                    .commit();
            break;
    }
}

And, finally adding the each children fragments using: The layout file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">
    <FrameLayout
        android:id="@+id/content_container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/colorAccent"
        android:layout_weight="8"
        ></FrameLayout>

    <FrameLayout
        android:id="@+id/footer_container"
        android:background="@color/colorPrimaryDark"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="2"
        ></FrameLayout>
</LinearLayout>

The children fragments adding:

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

        View view = inflater.inflate(R.layout.primary_fragment, container, false);
        getChildFragmentManager()
                .beginTransaction()
                .add(R.id.content_container, new PrimaryContentFragment())
                .add(R.id.footer_container, new PrimaryFooterFragment())
                .addToBackStack("primarychildren")
                .commit();

        return view;
    }

I am adding the similar logics for the another fragment also and so on for the rest which is working.

THE PROBLEM: The solution as stated is working but seems as very raw naive approach. Could anybody suggest the better architecture I could follow such as:

  • all the fragments (Primary/Secondary/...) uses the same designs so can I create some base class to inherit common features
  • all the footer are similar for all of fragments but with simple text change (might be like breadcrumb) So, Can I use same footer for all fragments with some settext method ...
  • how can I effectively communicate between activity and fragments

BEING A NEW ANDROID DEV, MY ONLY CONCERN IS AM I DOING THE RIGHT WAY !!!

OmGanesh
  • 952
  • 1
  • 12
  • 24

2 Answers2

1

Try this using bundle :-

  ContentFragment content=new ContentFragment();
  content.setArguments( ( new Bundle()).putString("value","primary"));

getChildFragmentManager()
            .beginTransaction()
            .add(R.id.content_container, content)
            .add(R.id.footer_container, new PrimaryFooterFragment())
            .addToBackStack("primarychildren")
            .commit();

Similarly For secondary use :-

  ContentFragment content=new ContentFragment();
  content.setArguments( ( new Bundle()).putString("value","secondary"));

Use same content and footer fragments just set the texts by using bundle arguments

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
0

All the fragments (Primary/Secondary/...) uses the same designs so can I create some base class to inherit common features

If they are using the same design and with slight changes in their content, then there's no need of create different Fragment classes. You can just pass necessary values from your calling Activity or Fragment to populate the contents in each of your child fragments.

All the footer are similar for all of fragments but with simple text change (might be like breadcrumb) So, Can I use same footer for all fragments with some settext method ...

If they are just simple text changes, then please use the setArguments and getArguments methods to pass values between Fragments rather creating different Fragment classes.

Here's how you can pass values between fragments. And here's how you can pass data from your Activity to Fragment.

How can I effectively communicate between activity and fragments

Please follow the two links above to communicate between Activity and Fragment.

Update

Following up the comment, as you have said that the PrimaryFragment and SecondaryFragment are mostly likely, I would suggest you to have one single Fragment having all these. Instead of having PrimaryFragment, SecondaryFragment and a CommonFragment, you might consider a single Fragment having the footer Fragment as well. When you are about to launch an instance of that Fragment, just pass necessary values to populate data as the contents of those Fragment.

Please let me know if I have not clarified enough.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
  • @Murshed I was thinking if I could use some CommonFragment with contentFragment and FooterFragment classes inside them and inherit PrimaryFragment and SecondaryFragment from them so I don't even need to include footer and some property in CommonFragment will be used to change values for footer. (might be design flaw). Can we address that way? – OmGanesh Mar 18 '18 at 16:41
  • I tried to solve the problem such as: (1) mainactivity to instantiate commonfragment and add to its container (2) commonfragment responsibility do decide which fragment to draw in its container (3) Each individualfragment (eg PrimaryFragment) will add both contentfragment (unique) and footerfragment as single transaction into its view (4) footerfragment is the commonfooterfragment which will accept the arguments to show the fragment specific breadcrumb. [Sorry if you didn't get it ... the work i tried to do is at https://github.com/gitaarush/SOAndroidApp/tree/feature_design) – OmGanesh Mar 18 '18 at 19:43