I am making a ViewPager that contains 3 fragments.
Each fragment, upon startup, has to make an asynchronous call to retrieve certain data through a database query.
It does these calls well, the problem is that, once in this activity, if I pass from the left fragment to the central fragment, it makes the asynchronous call from the right fragment and if I pass from the right fragment to the central fragment, it makes the call async from the left fragment. I don't know if it's a context problem, 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 load the fragments into 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();
}
}
This happens because
ViewPager
they have a property calledsetOffscreenPageLimit()
that determines the number of "pages" that can be kept depending on the "page" you are on.Example:
3 Fragments A , B , C.
The
setOffscreenPageLimit
default value is 1 and it will always be greater than or equal to 1.setOffscreenPageLimit(1)
:If you are in fragment A , fragment
fragment
B will be created .If you change to
fragment
B ,fragment
A will remain active andfragment
C will be created .If you switch to
fragment
C ,fragment
A will be destroyed and B will remain active.This process happens the same from right to left so if you change from
fragment
C tofragment
B ,fragment
A will be created.If for example you change se
setOffscreenPageLimit
to 2 since you change tofragment
B the 3 fragments will always remain active.So to solve your problem you can use
setUserVisibleHint
:With this method you can know when a
fragment
is visible to the user, but be careful, you must check if you have already created thefragment
so as not to call the asynchronous method again every time you change fromfragment
.