android - Redrawing a Fragment after it becomes visible -
i have menufragment
extends fragment
support package. menufragment
can call fragment
settingsfragment
. in settingsfragment
use switch
'es enable or disable functionalities. if functionality disabled button specific functionality should not shown in menufragment
, if enabled should shown.
my problem oncreate
, oncreateview
, onresume
, etc. arent called when go menufragment
after changed settings in settingsfragment
. have restart app before changes have made taking effect in menufragment
.
is idea set callback settingsfragment
menufragment
, call invalidate()
on menufragment
's main content everytime change switch in settingsfragment
or there better solution out there?
edit
i tried use callback redraw fragment
noticed calling invalidate()
nothing. not force view redrawn... im wondering if have replace menufragment
menufragment
force oncreate
called.
the menuactivity
:
public class menuactivity extends navigationactivity { private fragmentmanager fragmentmanager = null; private menufragment fragment = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setrequestedorientation(activityinfo.screen_orientation_nosensor); this.fragmentmanager = this.getsupportfragmentmanager(); this.fragment = (menufragment) this.fragmentmanager.findfragmentbytag(fragmenttags.menu.getvalue()); if (savedinstancestate == null) { this.fragment = new menufragment(); this.fragmentmanager.begintransaction().replace(r.id.menu_slide_main_content, this.fragment, fragmenttags.menu.getvalue()).commit(); } } /** * */ @override protected void onresume() { log.v("menu", "onresume called"); super.onresume(); } /** * */ @override protected void onpause() { log.v("menu", "onresume called"); super.onpause(); } }
the menufragment
:
public class menufragment extends fragment implements ontouchlistener, ondraglistener, dropcallback { private sessionloginpreferences sessionloginpreferences = null; private sessionloginsingleton sessionloginsingleton = null; private sessionconfigpreferences sessionconfigpreferences = null; private circularlayout circleview = null; private imagebutton buttonmeasure = null; private imagebutton buttoncalc = null; private imagebutton buttonprofile = null; private imagebutton buttonrecipe = null; private imagebutton buttondiagrams = null; private imagebutton buttonmeasuredatalist = null; private imagebutton buttonfollower = null; private imagebutton buttonsettings = null; private imagebutton buttonlogout = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.sessionloginpreferences = new sessionloginpreferences(this.getactivity()); this.sessionloginsingleton = sessionloginsingleton.getinstance(this.getactivity()); this.sessionconfigpreferences = new sessionconfigpreferences(this.getactivity()); } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { this.buttonmeasure = new imagebutton(this.getactivity()); this.buttonmeasure.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); this.buttonmeasure.setimageresource(r.drawable.ic_measure); this.buttonmeasure.getbackground().setalpha(0); this.buttonmeasure.setontouchlistener(this); this.buttoncalc = new imagebutton(this.getactivity()); this.buttoncalc.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); this.buttoncalc.setimageresource(r.drawable.ic_calculator); this.buttoncalc.getbackground().setalpha(0); this.buttoncalc.setontouchlistener(this); this.buttonprofile = new imagebutton(this.getactivity()); this.buttonprofile.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); this.buttonprofile.setimageresource(r.drawable.ic_profile); this.buttonprofile.getbackground().setalpha(0); this.buttonprofile.setontouchlistener(this); this.buttonrecipe = new imagebutton(this.getactivity()); this.buttonrecipe.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); this.buttonrecipe.setimageresource(r.drawable.ic_scanner); this.buttonrecipe.getbackground().setalpha(0); this.buttonrecipe.setontouchlistener(this); this.buttondiagrams = new imagebutton(this.getactivity()); this.buttondiagrams.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); this.buttondiagrams.setimageresource(r.drawable.ic_diagrams); this.buttondiagrams.getbackground().setalpha(0); this.buttondiagrams.setontouchlistener(this); this.buttonmeasuredatalist = new imagebutton(this.getactivity()); this.buttonmeasuredatalist.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); this.buttonmeasuredatalist.setimageresource(r.drawable.ic_list); this.buttonmeasuredatalist.getbackground().setalpha(0); this.buttonmeasuredatalist.setontouchlistener(this); this.buttonfollower = new imagebutton(this.getactivity()); this.buttonfollower.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); this.buttonfollower.setimageresource(r.drawable.ic_follower); this.buttonfollower.getbackground().setalpha(0); this.buttonfollower.setontouchlistener(this); this.buttonsettings = new imagebutton(this.getactivity()); this.buttonsettings.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); this.buttonsettings.setimageresource(r.drawable.ic_settings); this.buttonsettings.getbackground().setalpha(0); this.buttonsettings.setontouchlistener(this); this.buttonlogout = new imagebutton(this.getactivity()); this.buttonlogout.setlayoutparams(new relativelayout.layoutparams(relativelayout.layoutparams.wrap_content, relativelayout.layoutparams.wrap_content)); this.buttonlogout.setimageresource(r.drawable.ic_logout); this.buttonlogout.getbackground().setalpha(0); this.buttonlogout.setontouchlistener(this); string email = this.getemail(); map<string, boolean> config = this.sessionconfigpreferences.getconfigdetails(email); list<view> views = new arraylist<view>(); views.add(this.buttonmeasure); views.add(this.buttonprofile); views.add(this.buttonsettings); views.add(this.buttonlogout); if(config.get(globals.config_calc)) { views.add(this.buttoncalc); } if(config.get(globals.config_recipe)) { views.add(this.buttonrecipe); } if(config.get(globals.config_diagrams)) { views.add(this.buttondiagrams); } if(config.get(globals.config_list)) { views.add(this.buttonmeasuredatalist); } if(config.get(globals.config_follower)) { views.add(this.buttonfollower); } this.circleview = new circularlayout(this.getactivity(), this, 250, views); this.circleview.setondraglistener(this); return this.circleview; } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); } /** * */ @override public void onresume() { log.v("menufragment", "onresume called"); super.onresume(); } /** * */ @override public void onpause() { log.v("menufragment", "onresume called"); super.onpause(); } @override public boolean ontouch(view view, motionevent event) { switch (event.getaction()) { case motionevent.action_down : { clipdata data = clipdata.newplaintext("", ""); imagebutton imagebutton = (imagebutton)view; view.dragshadowbuilder shadowbuilder = new view.dragshadowbuilder(imagebutton); view.startdrag(data, shadowbuilder, view, 0); view.setvisibility(view.invisible); } break; case motionevent.action_up: { view.performclick(); view.setvisibility(view.visible); } break; } return true; } @override public boolean ondrag(view view, dragevent event) { switch(event.getaction()) { case dragevent.action_drop : { view sourceview = (view) event.getlocalstate(); float sourcex = sourceview.getx(); float sourcey = sourceview.gety(); float dropx = event.getx() - (sourceview.getwidth() / 2); float dropy = event.gety() - (sourceview.getheight() / 2); sourceview.setx(dropx); sourceview.sety(dropy); sourceview.setvisibility(view.visible); translateanimation animation = new translateanimation(dropx - sourcex, 0, dropy - sourcey, 0); animation.setduration(300); sourceview.startanimation(animation); sourceview.setx(sourcex); sourceview.sety(sourcey); } break; } return true; } @override public boolean ondrop(view view, dragevent event) { switch(event.getaction()) { case dragevent.action_drop : { view sourceview = (view)event.getlocalstate(); sourceview.setvisibility(view.visible); if(sourceview.equals(this.buttonmeasure)) { this.performnfcresult(); } else if(sourceview.equals(this.buttoncalc)) { this.performcalc(); } else if(sourceview.equals(this.buttonprofile)) { this.performprofile(); } else if(sourceview.equals(this.buttonrecipe)) { this.performrecipe(); } else if(sourceview.equals(this.buttondiagrams)) { this.performdiagrams(); } else if(sourceview.equals(this.buttonmeasuredatalist)) { this.performmeasuredatalist(); } else if(sourceview.equals(this.buttonfollower)) { this.performfollower(); } else if(sourceview.equals(this.buttonsettings)) { this.performsettings(); } else if(sourceview.equals(this.buttonlogout)) { this.performlogout(); } } break; } return true; } private void performnfcresult() { intent newintent = new intent(this.getactivity(), measuredataactivity.class); newintent.putextra(globals.key_nfc, false); this.startactivity(newintent); } private void performcalc() { intent newintent = new intent(this.getactivity(), calcactivity.class); this.startactivity(newintent); } private void performprofile() { intent newintent = new intent(this.getactivity(), profileactivity.class); this.startactivity(newintent); } private void performrecipe() { intent newintent = new intent(this.getactivity(), recipeactivity.class); this.startactivity(newintent); } private void performdiagrams() { intent newintent = new intent(this.getactivity(), diagramsactivity.class); this.startactivity(newintent); } private void performmeasuredatalist() { intent newintent = new intent(this.getactivity(), measuredatalistactivity.class); this.startactivity(newintent); } private void performfollower() { intent newintent = new intent(this.getactivity(), followeractivity.class); this.startactivity(newintent); } private void performsettings() { intent newintent = new intent(this.getactivity(), settingsactivity.class); this.startactivity(newintent); } private void performlogout() { if(this.sessionloginpreferences.isloggedin()) { this.sessionloginpreferences.logout(); } else if(this.sessionloginsingleton.isloggedin()) { sessionloginsingleton.getinstance(null).logout(); } this.getactivity().finish(); } private string getemail() { string email = null; if (this.sessionloginpreferences.isloggedin()) { map<string, string> userdata = this.sessionloginpreferences.getuserdetails(); email = userdata.get(globals.key_email); } else if (this.sessionloginsingleton.isloggedin()) { email = this.sessionloginsingleton.getemail(); } return email; } }
the settingsactivity
public class settingsactivity extends navigationactivity { private fragmentmanager fragmentmanager = null; private settingsfragment fragment = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.fragmentmanager = this.getsupportfragmentmanager(); this.fragment = (settingsfragment) this.fragmentmanager.findfragmentbytag(fragmenttags.settings.getvalue()); if (savedinstancestate == null) { this.fragment = new settingsfragment(); this.fragmentmanager.begintransaction().replace(r.id.menu_slide_main_content, this.fragment, fragmenttags.settings.getvalue()).commit(); } } }
the settingsfragment
public class settingsfragment extends fragment implements oncheckedchangelistener { private sessionconfigpreferences sessionconfigpreferences = null; private sessionloginpreferences sessionloginpreferences = null; private sessionloginsingleton sessionloginsingleton = null; private switch switchdiagrams = null; private switch switchrecipe = null; private switch switchcalc = null; private switch switchlist = null; private switch switchfollower = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); this.setretaininstance(true); this.getactivity().getwindow().setsoftinputmode(windowmanager.layoutparams.soft_input_state_always_hidden); this.sessionconfigpreferences = new sessionconfigpreferences(this.getactivity()); this.sessionloginpreferences = new sessionloginpreferences(this.getactivity()); this.sessionloginsingleton = sessionloginsingleton.getinstance(this.getactivity()); this.sethasoptionsmenu(true); } @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { return inflater.inflate(r.layout.activity_settings_fragment, container, false); } @override public void onviewcreated(view view, @nullable bundle savedinstancestate) { super.onviewcreated(view, savedinstancestate); this.switchdiagrams = (switch) view.findviewbyid(r.id.settings_switch_diagrams); this.switchdiagrams.setoncheckedchangelistener(this); this.switchrecipe = (switch) view.findviewbyid(r.id.settings_switch_recipe); this.switchrecipe.setoncheckedchangelistener(this); this.switchcalc = (switch) view.findviewbyid(r.id.settings_switch_calc); this.switchcalc.setoncheckedchangelistener(this); this.switchlist = (switch) view.findviewbyid(r.id.settings_switch_measuredata_list); this.switchlist.setoncheckedchangelistener(this); this.switchfollower = (switch) view.findviewbyid(r.id.settings_switch_follower); this.switchfollower.setoncheckedchangelistener(this); } @override public void onresume() { super.onresume(); string email = getemail(); map<string, boolean> config = this.sessionconfigpreferences.getconfigdetails(email); this.switchdiagrams.setchecked(config.get(globals.config_diagrams)); this.switchrecipe.setchecked(config.get(globals.config_recipe)); this.switchcalc.setchecked(config.get(globals.config_calc)); this.switchlist.setchecked(config.get(globals.config_list)); this.switchfollower.setchecked(config.get(globals.config_follower)); } @override public boolean onoptionsitemselected(menuitem item) { switch (item.getitemid()) { case android.r.id.home: this.getactivity().finish(); return true; default: return super.onoptionsitemselected(item); } } @override public void oncheckedchanged(compoundbutton view, boolean checked) { string email = getemail(); switch(view.getid()) { case r.id.settings_switch_diagrams: this.sessionconfigpreferences.configure(email, globals.config_diagrams, checked); break; case r.id.settings_switch_recipe: this.sessionconfigpreferences.configure(email, globals.config_recipe, checked); break; case r.id.settings_switch_calc: this.sessionconfigpreferences.configure(email, globals.config_calc, checked); break; case r.id.settings_switch_measuredata_list: this.sessionconfigpreferences.configure(email, globals.config_list, checked); break; case r.id.settings_switch_follower: this.sessionconfigpreferences.configure(email, globals.config_follower, checked); break; } } private string getemail() { string email = null; if (this.sessionloginpreferences.isloggedin()) { map<string, string> userdata = this.sessionloginpreferences.getuserdetails(); email = userdata.get(globals.key_email); } else if (this.sessionloginsingleton.isloggedin()) { email = this.sessionloginsingleton.getemail(); } return email; } }
i guess following best solution kind of problem:
/** * */ @override public void onbackpressed() { intent intent = new intent(this, menuactivity.class); intent.addflags(intent.flag_activity_clear_top); intent.addflags(intent.flag_activity_clear_task); intent.addflags(intent.flag_activity_new_task); this.startactivity(intent); this.finish(); }
adding code above settingsactivity
causes old menuactivity
killed , starts new menuactivity
needs add menufragment
, oncreate
, oncreateview
etc. called view acts got updated.
this works api level >= 5.0
older api's should use:
@override public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { intent intent = new intent(this, menuactivity.class); intent.addflags(intent.flag_activity_clear_top); intent.addflags(intent.flag_activity_clear_task); intent.addflags(intent.flag_activity_new_task); this.startactivity(intent); this.finish(); return true; } return super.onkeydown(keycode, event); }
let me know if isnt solution. if please provide informations why.
Comments
Post a Comment