My static handler has a WeakReference for my Activity (this is necessary to prevent a problem with documented memory).
I am sending a message with a long delay and I want this message to reach my activity (which should be in the foreground).
My concern is that when the orientation changes, my activity is destroyed, and the handler refers to the old activity that was supposed to be destroyed.
To get around this in my onCreate for activity, I do this.
if(mHandler == null) mHandler = new LoginHandler(this); else { mHandler.setTarget(this); }
And my handler is declared as a static global variable:
private static LoginHandler mHandler = null;
and the implementation class is also static, as shown below:
private static class LoginHandler extends Handler { private WeakReference<LoginActivity> mTarget; LoginHandler(LoginActivity target) { mTarget = new WeakReference<LoginActivity>(target); } public void setTarget(LoginActivity target) { mTarget = new WeakReference<LoginActivity>(target); } @Override public void handleMessage(Message msg) {
What do I want to know if something is wrong with changing WeakReference to onCreate or something else is wrong with this approach?
Thanks,
source share