Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle network calls inside a Fragment

Tags:

android

I have the following situation:

I have an Activity that hosts a ViewPager, and I have 4 Fragments;

the ViewPager at the beginning contains Fragment A,

when the user swipes on the ViewPager Fragment B goes into the ViewPager, then Fragment C and Fragment D ...etc...

Now as soon as the FragmentPagerAdapter is instantiated at least 2 of the Fragments are created.

This poses 2 problem:

  1. Every Fragment needs to perform network calls, but I do not want to do unnecessary ones (I do not want to make network calls for Fragment B, if the user never swipes to Fragment B );
  2. similar to 1.), I need to show a ProgessDialog when a Fragment perform network calls, but I do not want to show dialogs from Fragment B if the user never goes to it...

Please what kind of pattern should I use in such a circumstance?

Activity

public class PagerActivity extends ActionBarActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.viewpager_layout);

ViewPager pager=(ViewPager)findViewById(R.id.pager);
TabPageIndicator tabs=(TabPageIndicator)findViewById(R.id.titles);

pager.setAdapter(buildAdapter());
tabs.setViewPager(pager);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}

FragmentPagerAdapter

public class MyFragmentPagerAdapter extends FragmentPagerAdapter {


@Override
public int getCount() {
    return (4);
}

@Override
public Fragment getItem(int position) {


    if (position == 1) {
        if (dashbardFragment == null)
            dashbardFragment = DashBoardFragment.newInstance(position);
        return dashbardFragment;
    }
    if (position == 0) {
        if (listOfParticipantFragment == null)
            listOfParticipantFragment = ListOfParicipantsFragment
            .newInstance(position);
        return listOfParticipantFragment;
    }

}

1 Fragment

public class ListOfParicipantsFragment extends Fragment {

public static ListOfParicipantsFragment newInstance(int position) {
    ListOfParicipantsFragment frag = new ListOfParicipantsFragment();
    return (frag);
}

public static String getTitle(Context ctxt, int position) {
    return myApplication.getContext().getResources().getString(R.string.list_of_participants_fragment_title);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View result = inflater.inflate(R.layout.guest_list_fragment_layout,
            container, false);

    return (result);
}
like image 612
Lisa Anne Avatar asked Oct 16 '14 08:10

Lisa Anne


People also ask

How do you call an activity inside a fragment?

Best way of calling Activity from Fragment class is that make interface in Fragment and add onItemClick() method in that interface. Now implement it to your first activity and call second activity from there.

How do you pass information between fragments?

To pass data between fragments in the same fragment manager, the listener should be added to the destination fragment with requestKey in order to receive the result produces from another fragment with the same key.

Where do we call API in fragment?

If you app follows SingleActivity-MultipleFragments pattern, then you can call API related stuff in your Activity. You can roam around fragments, use the data fetched by Activity in those fragments, update the data as necessary.


2 Answers

Try this, in each fragment override below method and call your function when it is visible:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if(isVisible()){
        if(isVisibleToUser){
            Log.d("MyTag","My Fragment is visible");
        }else{
            Log.d("MyTag","My Fragment is not visible");
        }
    }
}

EDIT

Note: This is only useful when using a FragmentPagerAdapter or FragmentStatePagerAdapter

like image 105
mmlooloo Avatar answered Oct 19 '22 02:10

mmlooloo


Basically what you want to do, is, find which fragment is currently being viewed when you swipe. And then, do your network calls.

You can take advantage of the ViewPager listeners to get notified when the user swipes to a new page. Docs : http://developer.android.com/reference/android/support/v4/view/ViewPager.OnPageChangeListener.html#onPageSelected(int)

This will give you the position of the View. But i'm assuming that what you want is the actual fragment, which is a bit more tricky.

But, this has been answered already in here : Is it possible to access the current Fragment being viewed by a ViewPager?

Hope it helps

like image 39
ehanoc Avatar answered Oct 19 '22 04:10

ehanoc