android - Start Fragment from Activity -
i'm able start fragment activity. im worried potential problems implementation.
i have 2 fragments, fragmenta
, fragmentb
, have 3 activity classes, activity1
, activity2
, resultactivity
public class navigationtabs extends fragmentactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.simple_tabs_2); fragmentstatepageradapter adapter = new mytabs(getsupportfragmentmanager()); .... .... } static class mytabs extends fragmentstatepageradapter { public mytabs(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { switch(position) { case 0: fragmenta fraga = new fragmenta(); homecoming fraga; case 1: fragmentb fragb = new fragmentb(); homecoming fragb; ...... ...... } }
^how phone call fragmenta
, fragmentb
fragmenta
starts activity1
via intent.
intent intent = new intent(getactivity(), activity1.class); startactivity(intent);
activity1
passes results of counter resultactivity
resultactivity
starts(or returns to) fragmenta
, sets sharedpreferences via onclick this
public void onclick(view v) { if(v.getid()== r.id.button_1){ sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(this); editor edit = sp.edit(); edit.putint(passedsavepref, counter); edit.commit(); finish(); } }
fragmentb
starts activity2
via intent.
intent intent = new intent(getactivity(), activity2.class); startactivity(intent);
activity2
passes results of counter resultactivity
resultactivity
starts(returns to) fragmentb
, sets sharedpreferences via onclick this
public void onclick(view v) { if(v.getid()== r.id.button_1){ sharedpreferences sp = preferencemanager.getdefaultsharedpreferences(this); editor edit = sp.edit(); edit.putint(passedsavepref, counter); edit.commit(); finish(); } }
this works needs. gc seems fine, freeing , allocating memory.
resultactivty
returns right fragment, , sets savedpreferences correctly.
yet seems bad implementation. starters, while searching through other questions i've read, "don't start activity straight fragment" poster linked proper implementation here https://developer.android.com/guide/components/fragments.html#eventcallbacks
i've tried calling activity1
fragmenta
this, don't see difference in behavior or performance
intent intent = new intent(getactivity(), activity1.class); startactivity(intent); getactivity().getsupportfragmentmanager().popbackstack();
so question is, need finish/remove fragmenta
when start activity1
, start fragmenta
1 time again resultactivity
using like
fragmenttransaction transaction = getfragmentmanager().begintransaction();
thanks in advance.
edit trying either kill/finish/pop fragmenta
re-start resultactivity
. reason tying because savedpreferences not loading when going fragmenta
resultactivity
.(well saving , loading correctly, couldn't see them)
as understand docs,fragments go on pause. calling loadpreferences method onresume(); loaded savedpreferences.
not marking answer, because did not implement of standard/proper practices of dealing fragments popbackstack();
fragmenttransactions
etc
quote: "don't start activity straight fragment"
i read poster wrote this, , disagree. rationale reduces modularity of fragment, , believes should impelment interface phone call activity.
i disagree. doesn't cut down modularity, in fact increases it. why implement layer of abstraction fragment intended in every implementation? why re-write same code in every activity, when can modularized in fragment? in fact fragments wouldn't have own specialized functions starting activities if against design principles.
for instance, if start activity using fragment.startacitivtyforresult(), fragment set straight onactivityresult receiver. not if utilize activity instance straight start new activity.
you not need remove framgents before starting new activityandroid pause/stop fragments, , potentially destroy them , underlying activity if need be. android destroying , recreating activities , fragments, every time alter orientation on screen - default settings has activity , fragments commit mass suicide.
this why functions onsaveinstancestate() important, because allow activities , fragments homecoming saved state. general rule of android programming activity / fragments should able respond spontaneous death gracefully.
android android-intent android-fragments fragmenttransaction
No comments:
Post a Comment