java - Showing and hiding SettingsFragment in an activity -
so followed this guide on android developers. suggest use fragments showing settings user.
i created xml , strings , fragment:
public class settingsfragmentapp extends preferencefragment{ @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); addpreferencesfromresource(r.xml.preferences_app); } }
i want show on mainactivity page without creating activity hosts fragment (i think later optioon recommended google kills point...why should create activity fragment?). added option menu
, handle in mainactivity
:
//inside onoptionsitemselected(menuitem item) case (r.id.action_settings_user): getfragmentmanager().begintransaction().replace(android.r.id.content, new settingsfragmentuser()).commit(); return true;
this way setting fragments shows expected, user hits button application exits because still on mainactivity
.
so question how can handle button in such way saves settings changes , brings user mainactivity?
if want button functionality have add fragment
stack in transaction.
fragmentmanager manger = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace(r.id.flfragmentcontainer, fragment); transaction.addtobackstack(null); // add fragment stack. transaction.commit();
but not recommended. reason suggest use activity
can build navigation stack activities
instead of fragments. building navigation stack fragments
can become problematic quickly. activities
supposed container fragments
. such in app going have many activities
not contain aside fragment
, these activities
used build navigation stack. in big apps tend write 1 abstract base activity
implements base features need , reuse on activity
as possible.
Comments
Post a Comment