i got this issue, i have a handler in Activity A that is use for show some message depending on the Message.what attribute, ok so far, i got a second Activity (Activity B) which is started by Activity A, i need the same handler from Activity A to use it on Activity B (i tried so many things and google it and nothing) i end up copying the code of Handler on A to B, but i really know this is so WRONG (not sure if necessary for handlers), here i will post some of the solutions i found and tried:
static variable :Accessing instance of the parent activity?, it works, but sometimes the static members go to null since the class is not loaded anymore, since somes said that this could bring up some leaks about the static member staying on memory i did some trick with Activity state (setting it to null when Activity A calls onDestroy, setting the reference "static variable" = this on methods onResume and onCreate of Activity A, after this, still the code comes with the expected NullPointerException at the "static variable" when we lose the loaded class.
another thing i tried was MyOwnHandler (a class created by me extending Handler and implementing Serializable trying to pass it through Intent.putExtra), but i noticed just when i think about that idea that Handler had some of it's method with final.
Any suggestion for this issue? i want to reuse the Handler on Activity A to Activity B, TIP: this could be done if i got the exact running instance of Activity A in Activity B code, if need some examples i could bring some just in case they are strictly necessary.
EDIT: As needed.
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
Toast toast;
switch (msg.what) {
case 3:
toast = Toast.makeText(getBaseContext(),
"The user is already 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 be found in database.",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL
| Gravity.CENTER_VERTICAL, 0, 0);
toast.show();
break;
}
}
};
this Handler member is private, could be public or default but still won't be relevant. Ok let's say this code is in Activity A, and i want to use the same exact code for Activity B, Activity B is started by A via Intent, i'm trying today @pskink answer, seems like that's the solution, anyway i'd still listening to propositions.
Regards.