Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Viewpager and fragments, methods not working

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?

like image 382
e_ori Avatar asked Sep 08 '12 07:09

e_ori


1 Answers

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...

like image 136
e_ori Avatar answered Oct 04 '22 20:10

e_ori