I'm doing a ViewPager that contains 3 fragments.
Each fragment, when starting, has to make an asynchronous call to retrieve certain data through a query to a database.
These calls are good, the problem is that, once in this activity, if I go from the left fragment to the central fragment, it makes me the asynchronous call of the fragment of the right and if I go from the fragment of the right to the middle one, performs the asynchronous call of the fragment on the left. I do not know if it is a problem of the contexts, since to do certain things I need to call the context (in the 3 fragments I have to use the context) and I get it by calling getContext ().
This is how I charge the fragments in the ViewPager:
public class LoraPrincipalSlideActivity extends FragmentActivity {
ViewPager pager = null;
MyFragmentPagerAdapter pagerAdapter;
@Override
protected void onCreate(Bundle arg0) {
arg0 = getIntent().getExtras();
super.onCreate(arg0);
this.setContentView(R.layout.activity_lora_principal_slide);
this.pager = (ViewPager) this.findViewById(R.id.pager);
MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
adapter.addFragment(AccionesActivityLoraFragment.newInstance(arg0)); //Añadimos el fragment de las acciones
adapter.addFragment(LoraFragmentActivity.newInstance(arg0)); //Añadimos el fragment del lora
adapter.addFragment(DatosFragmentActivity.newInstance(arg0)); //Añadimos el fragment de los datos
this.pager.setAdapter(adapter);
//this.pager.setCurrentItem(1); //Iniciamos el ViewPager en el fragment de Lora
}
@Override
public void onBackPressed() {
/*if (this.pager.getCurrentItem() == 0)
super.onBackPressed();
else
this.pager.setCurrentItem(this.pager.getCurrentItem() - 1);
*/
super.onBackPressed();
}
}
This is my adapter:
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
List<Fragment> fragments;
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
this.fragments = new ArrayList<Fragment>();
}
public void addFragment(Fragment fragment) {
this.fragments.add(fragment);
}
@Override
public Fragment getItem(int arg0) {
return this.fragments.get(arg0);
}
@Override
public int getCount() {
return this.fragments.size();
}
}