I am using a ViewPager
with a custom PagerFragmentAdapter
that uses an internal list to provide fragments. I would like to add and remove fragments dynamically on settings changes.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
mAdapter = new ServicePagerAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
I have tried to call again the setAdapter
but I obtain an IllegalStateException (Can not perform this action after onSaveInstanceState)
.
How can achieve my purpose?
You don't need to reset your ServicePagerAdapter
every time. It's enough to create and set it once in onCreate()
method of your Activity
(or in onActivityCreated()
method of your Fragment
) and then you can simply call add()
method of your adapter to add new elements to it. But don't forget to call notifyDataSetChanged()
after every addition to adapter.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With