java - Android when fragment is removed -
i have framelayout , put there fragments click on button, next click should remove fragment framelayout, removeallviews()
(framelayout in fragment translaction method in activity). need action when removeallviews()
starts , have in fragment class goes wrong.
i tried: ondestroy()
ondestroyview()
onpause()
in fragment class
but works like:
- put fragment in framelayout (from activity)
- use
removeallviews()
(from activity) - there no fragment in framelayout (is clear) nothing else happens , methods not working
- put new fragment in framelayout (from activity) - methods (
ondestroy()
fragment class) works (probably it's real timedestroy
old fragment)
how possible 'get moment' when fragment not exists user? want send information server if user hides fragment.
@edit3 code method activity want make translaction
public void showproductslist(string producttype,int containerid){ list<string> prodnames = new arraylist<string>(); list<long> prodids = new arraylist<long>(); databasedaoprodprotein dao = new databasedaoprodprotein(getapplicationcontext()); dao.open(); list<databaseproduct> productlist = dao.getallproducts(); for(int i=0;i<productlist.size();i++){ prodnames.add(productlist.get(i).getname()); prodids.add(productlist.get(i).getid()); } dao.close(); productslist productslist = new productslist(producttype,prodnames,prodids); productslist.setonsystemuivisibilitychangelistener (new view.onsystemuivisibilitychangelistener() { @override public void onsystemuivisibilitychange(int visibility) { // note system bars "visible" if none of // low_profile, hide_navigation, or fullscreen flags set. toast.maketext(getapplicationcontext(),"action1 " ,toast.length_long).show(); if ((visibility & view.system_ui_flag_fullscreen) == 0) { // todo: system bars visible. make desired // adjustments ui, such showing action bar or // other navigational controls. toast.maketext(getapplicationcontext(),"action2 " ,toast.length_long).show(); } else { // todo: system bars not visible. make desired // adjustments ui, such hiding action bar or // other navigational controls. toast.maketext(getapplicationcontext(),"action3 " ,toast.length_long).show(); } } }); fragmentmanager manager = getsupportfragmentmanager(); fragmenttransaction transaction = manager.begintransaction(); transaction.replace(containerid, productslist).commit(); }
i used method in fragment by:
((mainactivity) getcontext()).showproductslist("carb", carbcontainer.getid());
there error:
error:(560, 21) error: cannot find symbol method setonsystemuivisibilitychangelistener(<anonymous onsystemuivisibilitychangelistener>)
you say:
"how possible 'get moment' when fragment not exists user? want send information server if user hides fragment."
i know did not mean "hide", use ondestroy()
method.
try trigger "hide"
view toplevellayout = findviewbyid(r.id.top_layout); toplevellayout.setvisibility(view.invisible);
you cannot go stopped state while fragment (activity) visible. android destroying activities, killing processes
the best way make sure runs via view run via post:
toplevellayout.post(new runnable() { @override public void run() { toplevellayout.removeallviews(); } }
to notified of system ui visibility changes, register view.onsystemuivisibilitychangelistener
view
(fragment).
https://developer.android.com/training/system-ui/visibility.html
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); toast.maketext(getcontext(),"action0 " ,toast.length_long).show(); fragment your_frag = new productslist(producttype,prodnames,prodids); getsupportfragmentmanager().begintransaction().replace(containerid,your_frag).commit(); getsupportfragmentmanager().executependingtransactions();//make sure oncreateview has executed your_frag.getrootview().setonsystemuivisibilitychangelistener (new view.onsystemuivisibilitychangelistener() { @override public void onsystemuivisibilitychange(int visibility) { // note system bars "visible" if none of // low_profile, hide_navigation, or fullscreen flags set. toast.maketext(getcontext(),"action1 " ,toast.length_long).show(); if ((visibility & view.system_ui_flag_fullscreen) == 0) { // todo: system bars visible. make desired // adjustments ui, such showing action bar or // other navigational controls. toast.maketext(getcontext(),"action2 " ,toast.length_long).show(); } else { // todo: system bars not visible. make desired // adjustments ui, such hiding action bar or // other navigational controls. toast.maketext(getcontext(),"action3 " ,toast.length_long).show(); } } }); }
a typical fragment looks this:
public class homefragment extends fragment { view mrootview = null; public homefragment(){}//null constructor @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { mrootview = inflater.inflate(r.layout.fragment_home, container, false); return mrootview ; } public view getrootview () { return mrootview; } }
Comments
Post a Comment