android - FragmentAdapter doesn't hold the good reference of the fragment -
i'm trying work fragment, i'm stuck issue can't solve.
i have 1 activity, holds 4 different fragment. activity, launch asynctask goes web , different data need, , send fragments.
but, when app gets killed , opened again, or when change orientation, fragments apparently recreated , custom fragmentadapter doesn't hold reference fragment.
here code of main activity.
public class mainactivity extends fragmentactivity { mypageradapter fgsadapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); //... fragmentmanager fm = super.getsupportfragmentmanager(); fgsadapter = new mypageradapter(fm,this); viewpager mypager = (viewpager) findviewbyid(r.id.home_pannels_pager); mypager.setadapter(fgsadapter); mypager.setcurrentitem(0); } @override protected void onresume() { super.onresume(); ataskgetuser task = new ataskgetuser(callback, (progressbar) findviewbyid(r.id.pb_asynctask)); task.execute(); } //what's called asynctask onpostexecute() private void notifydatachanged() { fgsadapter.notifyfragments(user.getitems()); } private class mypageradapter extends fragmentpageradapter { private list<cardfragment> fragments = new arraylist<cardfragment>(); private context c; public mypageradapter(fragmentmanager fm, context c) { super(fm); cardfragment h = new habitfragment(); cardfragment d = new dailyfragment(); cardfragment t = new todofragment(); cardfragment r = new rewardfragment(); fragments.add(h); fragments.add(d); fragments.add(t); fragments.add(r); } public int getcount() { return fragments.size(); } @override public cardfragment getitem(int position) { log.v("mainactivity_fgsmanager", "getitem()"); cardfragment f = (cardfragment) this.fragments.get(position); return f; } public void notifyfragments(list<habititem> items) { for(ontaskschanged f : fragments) { f.onchange(items); } } } }
so, want able do, able call onchange (an interface implemented 4 fragments), in notifydatachanged
function. possible, thinking wrong way?
i got same problems once fragments, losing current fragment after every screen rotation.
i solved adding 1 line in fragment class (not in parent fragmentactivity class):
@override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view v = inflater.inflate(r.layout.rate_fragment, container,false); setretaininstance(true);//added not lose fragment instance during screen rotations (...)
for case app gets killed , opened again, not sure work though.
Comments
Post a Comment