I have main an Activity class, a broadcast class ( subclass of broadcast receiver ) and few other classes. I use a Handler created in the broadcast class, for some things to do in future. But if some circumstances come ( user wants to exit app ), i want that Handler to cancel (prevent from being executed ).
I read a lot of threads in SO on how to cancel Handlers and I know how to from same class ( Handler.removeCallback (Runnable ), Handler.removeMessages(0) etc ). But I don’t know how to cancel it from my Activity. ( user presses exit button, and if handler is going to do some work i want to prevent that ).
So how do I reference that handler object (which is going to execute ) from the Activity class ?
I’m not sure I fully understand your question, however, if you want to cancel the
Handlervia an activity, you can put it in theonPausemethod:The
onPausemethod is called when an activity is about to be hidden from the user.Having re-read your question, if you mean that you want to access the handler inside a
BroadcastReceiverclass from yourActivityclass, then you should make the handler a member variable of your activity.You can use the handler inside your
BroadcastReceiverandActivityclass usingmHandler. You have to make sure that yourBroadcastReceiverclass is not astaticinner class of theActivity.