After a lot of efforts I finally did find the solution:
The Activity was destroyed when calling setAdapter
because it's creating a new Object of the adapter (as I assign the adapter as new object):
new LightPageAdapter(getChildFragmentManager(), fragments)
and that was destroying the old instance including the activity that it's initialization depended on this instance.
So that, I do split the fragmenting()
method into two parts , the first part when calling it for the first time in order to make the first initialization, and the second part when only the data changed and no need to recreate a new instance of the FragmentStatePagerAdapter
:
private static LightPageAdapter lpa;
private static PagerAdapter adapter;
public void fargmenting(List<Fragment> fragments, boolean first_init){
if(first_init){
ViewPager view_pager = (ViewPager) MyFragment.view.findViewById(R.id.lighting_pager);
lpa = new LightPageAdapter(getChildFragmentManager(), fragments);
}else{
LightPageAdapter.myFragments = fragments; // here I'm setting the fragments manually instead of passing it into a new constructor
lpa.notifyDataSetChanged();
}
adapter = lpa;
view_pager.setAdapter(adapter);
}