changeorientation call onpageselected before oncreateview
Asked Answered
J

3

7

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?

Judicature answered 5/6, 2014 at 22:34 Comment(0)
D
0
    private class CustomPageChangeListener extends ViewPager.SimpleOnPageChangeListener{

    @Override
    public void onPageSelected(int position) {
        // call super.onPageSelected() before onShowedFragment()
        super.onPageSelected(position);
        fragments.get(position).onShowedFragment(context);
       
    }
}

//initialize views in onCreateView()

databaseService = DatabaseService.getInstance(context);
mAdapter = new Capitulo100ListAdapter(context);
databaseService.capitulo100ObjectSet.setAdapter(mAdapter);

//if list is not null, setAdapter.

   @Override
public void onShowedFragment(Context context) {
    if(lst!=null)
    lst100.setAdapter(mAdapter);

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

//if still it's not working use MVVM architecture component .it will handle all issues related to activity recreation.

Drastic answered 15/6, 2021 at 13:40 Comment(0)
F
0

If you want to call some method whenever the fragment is shown to the user, please use setUserVisibleHint which is called from adapter whenever corresponding Fragment is shown.

Fetterlock answered 21/6, 2021 at 13:54 Comment(0)
E
0

It seems like you are either not initializing lst100 after activity's recreation or initializing it wrong anyways try getting a reference to the ListView in onShowedFragment itself.

@Override
public void onShowedFragment(Context context) {
    ...
    
    ListView listView = findViewById<ListView>(R.id.your_listview_id);
    listView.setAdapter(mAdapter);

    ...
    super.onShowedFragment(context);
}
Entophyte answered 21/6, 2021 at 15:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.