I have a ViewPager with two Fragments that I create in the onCreate my FragmentActivity .
private List<Fragment> fragments = new Vector<Fragment>(); fragments.add(Fragment.instantiate(this,Frag_1.class.getName())); fragments.add(Fragment.instantiate(this,Frag_2.class.getName())); this.vPagerAdapter = new Adapt(super.getSupportFragmentManager(),fragments); vPager = (ViewPager) super.findViewById(R.id.pager); vPager.setAdapter(vPagerAdapter);
My second Fragment has a method inside that I call to update my ListView - refreshList() :
public class Frag_2 extends Fragment { private ListView list; private ArrayList<data> data; private boolean firstCreation=true; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setRetainInstance(false); } public void onAttach(Activity activity) {
Called from my FragmentActivity
//Something Frag_2 fr = (Frag_2) vPagerAdapter.getItem(1); if (fr.getView() != null) { fr.refreshList(data); }
It works fine until I change the screen orientation. Correct me if I am wrong, but I was looking for a clock, and I did not find a solution or a good explanation, FragmentActivity is created only once, and Fragments bound to it, but Fragments recreate configuration changes. Now that the orientation is changing, I am not getting View from onCreate , so when I try to get View from Fragment , it returns null, and the refreshList() method is not called. How can i fix this?
source share