Handler: repeating code on android -
i got issue, have handler in activity use show message depending on message.what attribute, ok far, got second activity (activity b) started activity a, need same handler activity use on activity b (i tried many things , google , nothing) end copying code of handler on b, know wrong (not sure if necessary handlers), here post of solutions found , tried:
static variable :accessing instance of parent activity?, works, static members go null since class not loaded anymore, since somes said bring leaks static member staying on memory did trick activity state (setting null when activity calls ondestroy, setting reference "static variable" = on methods onresume , oncreate of activity a, after this, still code comes expected nullpointerexception @ "static variable" when lose loaded class.
another thing tried myownhandler (a class created me extending handler , implementing serializable trying pass through intent.putextra), noticed when think idea handler had of it's method final.
any suggestion issue? want reuse handler on activity activity b, tip: done if got exact running instance of activity in activity b code, if need examples bring in case strictly necessary.
edit: needed.
private handler mhandler = new handler() { public void handlemessage(message msg) { toast toast; switch (msg.what) { case 3: toast = toast.maketext(getbasecontext(), "the user saved.", toast.length_long); toast.setgravity(gravity.center_horizontal | gravity.center_vertical, 0, 0); toast.show(); break; case 2: toast = toast.maketext(getbasecontext(), "the entered e-mail couldn't found in database.", toast.length_long); toast.setgravity(gravity.center_horizontal | gravity.center_vertical, 0, 0); toast.show(); break; } } };
this handler member private, public or default still won't relevant. ok let's code in activity a, , want use same exact code activity b, activity b started via intent, i'm trying today @pskink answer, seems that's solution, anyway i'd still listening propositions.
regards.
what trying done using messenger http://developer.android.com/reference/android/os/messenger.html
Comments
Post a Comment