kerollos
kerollos

Reputation: 87

How to refresh data in ViewPager Fragments?

I need to know the life-cycle of the Fragments inside ViewPager? I have a ViewPager of 3 tabs (3 Fragments ) What is the life-cycle while swiping between them ?

I notice that when i go from Fragment 3 to Fragment 1 , it is being refreshed. so which method in the life-cycle is called in this moment?

I tried to put Toast inside every method, but i found that they are called after each other so i did`t know which one is responsible for refreshing.

I guess it`s onActivityCreated() , but to call it i must pass Bundle saveInstancestate. When i pass null it crashes, so how to get this object ?

If there any other/better approach to do so, please share. The goal is to refresh the data inside the fragment when i click on button in the actionbar.

Thanks

Upvotes: 0

Views: 1499

Answers (2)

nebuchadnezzar I
nebuchadnezzar I

Reputation: 125

In your activity, after you set the adapter for the view pager, put your code for whatever on the onPageSelected() method. It is called everytime you change or swipe the viewPager.

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

Upvotes: 0

Cory Roy
Cory Roy

Reputation: 5609

A view pager fragment always loads its neighbors on either side.

Because of this you will see the creation callbacks for the fragments run before they are visible. After you are 2 pages away from a fragment it will be destroyed.

Have your activity implement ViewPager.OnPageChangeListener then use onPageSelected to get the call into your fragment. You can keep references to the view page fragments in that activity.

 public class ViewPagerActivity implements ViewPager.OnPageChangeListener {

    public void onCreate(Bundle savedInstanceState) {
        ...
        if (savedInstanceState != null) {
           //Restore the fragment's instance
           mContent = getSupportFragmentManager().getFragment(savedInstanceState, "mContent");
           ...
        }
        ...
    }

   @Override
   protected void onSaveInstanceState(Bundle outState) {
      super.onSaveInstanceState(outState);

      //Save the fragment's instance
      getSupportFragmentManager().putFragment(outState, "mContent", mContent);
    }


    // regular activity stuff plus view pager methods

    @Override
    public void onPageSelected(int arg0) {
       final ReadyFragment fragment = mAdapter.getItem(arg0);
       if (fragment.isVisible()) {
          fragment.ready();  //method inside your custom fragment code
       }
   }
 }

Add an interface class:

public interface ReadyInterface {
   public void ready();
}

Implement that interface in your fragments:

public class ReadyFragment extends Fragment implements ReadyInterface {

  //.. your regular fragment stuff

 @Override
 public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    ...
    if (savedInstanceState != null) {
        //Restore the fragment's state here
    }
 }
 ...
 @Override
 public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);

    //Save the fragment's state here
  }

   @Overide
   public void ready() { 
     // do your stuff here that needs to happen 
     //once fragment is displayed and running
   }

} 

Upvotes: 1

Related Questions