Problem with fragments

1

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();
}
}
    
asked by Pablo Simon DiEstefano 21.12.2017 в 10:07
source

1 answer

0

This happens because the ViewPager have a property called setOffscreenPageLimit() that determines the number of "pages" that can be preserved depending on the "page" you are on.

Example:

3 Fragments A , B , C .

The setOffscreenPageLimit has the default value 1 and will always be greater than or equal to 1.

setOffscreenPageLimit(1) :

If you are in the fragment A the fragment B will be created.

If you change to fragment B the fragment A will remain active and the fragment C will be created.

If you change to fragment C the fragment A will be destroyed and B will remain active.

This process occurs the same from right to left so if the fragment C you change to fragment B , the fragment A will be created.

If for example you change the setOffscreenPageLimit to 2 since you change to fragment B the 3 fragments will always remain active.

Then to solve your problem you can use setUserVisibleHint :

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        // La llamada asíncrona aquí
    }else{
       // El fagment ya no es visible para el usuario
    }
} 

With this method you can know when a fragment is visible to the user, but beware, you must control if you have already created the fragment so as not to call the asynchronous method every time you change from fragment .

    
answered by 21.12.2017 / 12:17
source