6

I have a ViewPager with two Fragments which I instantiate in onCreate of 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) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
    }

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.layout, container, false);
        list = (ListView) view.findViewById(R.id.lst);
        //this.setRetainInstance(true);
        return view;
    }


    public void refreshList(ArrayList <data> data){
        if(data!=null){
            ArrayAdapter<data> adapter = new Item_data_adapter(getActivity(),data);
            list.setAdapter(adapter);}
    }
}

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 orientation of the screen. Correct me if I'm wrong, but I was searching for hours and I didn't find a solution or a good explanation, the FragmentActivity is created only one time and the Fragments are attached to it but the Fragments recreate on configuration changes. Now, when the orientation changes I don't get the View from onCreateso when I try to get the View from the Fragment it returns null and my refreshList() method isn't called. How can I fix this?

Cat
  • 66,919
  • 24
  • 133
  • 141
e_ori
  • 845
  • 1
  • 11
  • 29
  • I'm experiencing the same problem and haven't found a solution. I'm starting to think there's a bug in `FragmentManager` or something else considering we're both experiencing this. – adneal Sep 11 '12 at 02:29
  • what about your xml layout? this isn't a problem for static fragments declared in activity's layout but this isn't the case for dynamic fragments – chip Sep 12 '12 at 01:21
  • @zipc Both @DLock and I are using `ViewPager`. You don't write `Fragments` in your XML in that case. – adneal Sep 12 '12 at 01:26
  • I'm having the same problem. This question was asked 3 years ago, any update on this? I'm considering fixing my Activity to portrait because I'm tired of Android and it's undocumented fragment usage. I havent found anything on this and even though I retain my fragments Android still recreates the views then attaches the Activity again yet after flipping a page the fragment will have isAlive() as false. Does not make any sense to me. – breakline Dec 20 '15 at 18:57

3 Answers3

1

I fixed the problem this way: In the onCreate of the FragmentActivity

if(savedInstanceState!=null){
            frag1 = (frag_1) getSupportFragmentManager().getFragment(savedInstanceState, frag_1.class.getName());
            frag2 = (frag_2) getSupportFragmentManager().getFragment(savedInstanceState, frag_2.class.getName());
        }
        else{
            frag1 = (frag_1) Fragment.instantiate(this,frag_1.class.getName());
            frag2 = (frag_2) Fragment.instantiate(this,frag_2.class.getName());
        }
        fragments.add(frag1);
        fragments.add(frag2);


protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        getSupportFragmentManager().putFragment(outState, frag_1.class.getName(), frag1);
        getSupportFragmentManager().putFragment(outState, frag_2.class.getName(), frag2);
    }

Maybe it's not the best solution in the universe, but it looks like it works...

e_ori
  • 845
  • 1
  • 11
  • 29
  • This does work, also adjusting the `configChanges` in the Manifest works, but I don't think these are solid solutions. I suppose this way would work in the case that you have one or two `Fragments`, but I actually have five that are being effected by this. I'm glad it works for you though. – adneal Sep 12 '12 at 20:54
0

When u want to refresh the List do something like this :

public void setView() {
    Frag_2 fr = (Frag_2) vPagerAdapter.getItem(1);
    fragmentManager.beginTransaction().detach(fr).commit();
    fragmentManager.beginTransaction().attach(fr).commit();
}
Mr. S
  • 1,469
  • 2
  • 15
  • 27
ASH
  • 2,748
  • 2
  • 28
  • 29
-1

If you are using a dynamic fragment, you need to test first to prevent creating a second instance of a fragment.

To test whether the system is re-creating the activity, check whether the Bundle argument passed to your activity’s onCreate() is null.

If it is non-null, the system is re-creating the activity. In this case, the activity automatically re-instantiates existing fragments.

If it's null you can safely instantiate your dynamic fragment. For example:

public void onCreate(Bundle savedInstanceState) {
  // ...
  if (savedInstanceState != null) {
  FragmentManager fragmentManager = getFragmentManager()
  // Or: FragmentManager fragmentManager = getSupportFragmentManager()
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
  ExampleFragment fragment = new ExampleFragment();
  fragmentTransaction.add(R.id.fragment_container, fragment);
  fragmentTransaction.commit();
  }
}

The Fragment class supports the onSaveInstanceState(Bundle) method (but not the onRestoreInstanceState() method) in much the same way as the Activity class.

  • The default implementation saves the state of all the fragment’s views that have IDs.
  • You can override this method to store additional fragment state information.
  • If the system is re-creating the fragment from a previous saved state, it provides a reference to the Bundle containing that state to the onCreate(), onCreateView(), and onActivityCreated() methods; otherwise, the argument is set to null.

If you want a detailed info, here's a good talk by Ken Jones of Marakana

chip
  • 1,779
  • 1
  • 22
  • 34
  • This answer isn't relevant to the question. @DLock and I are not using `FragmentTransaction`, or static `Fragments` in our XML. We're using `ViewPager`, which calls for a `FragmentPagerAdapter`. – adneal Sep 12 '12 at 01:42