Tuesday, 15 June 2021

changeorientation call onpageselected before oncreateview

I have a ViewPager with 3 fragments; in my main Activity I handle onPageChangeListener:

private class CustomPageChangeListener extends ViewPager.SimpleOnPageChangeListener{

    @Override
    public void onPageSelected(int position) {

        fragments.get(position).onShowedFragment(context);
        super.onPageSelected(position);
    }
}

The call to onShowedFragment(context) loads data into fragment (I load data on demand). I am putting data loaded into a ListView:

@Override
public void onShowedFragment(Context context) {
    databaseService = DatabaseService.getInstance(context);
    mAdapter = new Capitulo100ListAdapter(context);
    databaseService.capitulo100ObjectSet.setAdapter(mAdapter);
    lst100.setAdapter(mAdapter);

    try {
        //load my data
    } catch (Exception e) {
        //...
    }
    super.onShowedFragment(context);
}

When I rotate the device, Android recreates the activity and calls onPageSelected, and that calls the method onShowedFragment on my fragment, but at the time the ListView called lst100 is null (I initialize the views in the method onCreateView of the fragment), then the line lst100.setAdapter(mAdapter) causes an error.

I am not using android:configChanges

How can I upload my data when the ListView is created after the rotation of the device?



from changeorientation call onpageselected before oncreateview

No comments:

Post a Comment