1

How can I refresh my fragment (chart-fragment) in parent fragment?
I tried doing it like this, but it doesn't work.

Java (refresh method):

Fragment fragment = getChildFragmentManager().findFragmentById(R.id.statistic_order_fragment);
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.detach(fragment);
    fragmentTransaction.attach(fragment);
    fragmentTransaction.commit();

XML:

<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ScrollView xmlns:card_view="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <fragment
                android:id="@+id/statistic_order_fragment"
                android:tag="ORDER"
                android:name="com.readyscript.dk.storemanagement.Statistic.StatisticOrderFragment"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_gravity="center_horizontal"
                card_view:layout="@layout/fragment_statistic_order" />

            <fragment
                android:id="@+id/statistic_sum_fragment"
                android:tag="SUM"
                android:name="com.readyscript.dk.storemanagement.Statistic.StatisticSumFragment"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:layout_gravity="center_horizontal"
                card_view:layout="@layout/fragment_statistic_sum" />

            <fragment
                android:id="@+id/statistic_check_fragment"
                android:tag="CHECK"
                android:name="com.readyscript.dk.storemanagement.Statistic.StatisticCheckFragment"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                android:layout_gravity="center_horizontal"
                card_view:layout="@layout/fragment_statistic_check" />

        </LinearLayout>
    </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

When I use SwipeRefreshLayout, I need to refresh all the 3 Fragments without restarting the parent Fragment.

Community
  • 1
  • 1

4 Answers4

4

After so much coding... I got solution for that..

 getActivity().getSupportFragmentManager().beginTransaction().replace(**containerId**,
                    new **NameOfCurrentFragment**).commit();
vivek modi
  • 51
  • 7
1

I think that the problem is that you have statically placed the fragments inside your xml file. A better approach is to put <FrameLayout>s instead of the <fragment> tags in your xml. Then, in your class, add the fragments programmatically, replacing them as needed.

Let me know if you need me to show you the code to do this.

AndroidDev
  • 20,466
  • 42
  • 148
  • 239
1

Use the getChildFragmentManager() instead of getFragmentManager().

Child Fragment Manager is precisely made to manage fragments inside of fragments, while Fragment Manger is there to manage fragments inside of Activities.

Replace,

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();

With this,

FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
Darshan Miskin
  • 844
  • 14
  • 25
1

to refresh fragment call below method

 @Override
public void setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint(isVisibleToUser);

  // Refresh tab data:

  if (getFragmentManager() != null) {
    getFragmentManager()
      .beginTransaction()
      .detach(this)
      .attach(this)
      .commit();
  }
}
AgentP
  • 6,261
  • 2
  • 31
  • 52