I’m dealing with legacy code. My task is to process the result of an async task.
I’m stuck with getting access to outer objects inside dispatchMessage method. Are there any means achieve it or should I use Singleton or static properties ?
So far I’ve made a class responsible for running in asynchronous mode.
private class BackgroundTask extends AsyncTask<Object, Void, Boolean>
{
@Override
protected Boolean doInBackground(Object... params){
// ...
}
@Override
protected void onPostExecute(Boolean result){
// ...
}
}
Inside the OnPostExecute or the doInBackground method I’d like to send a message using a handler, so I push in some simple stuff:
Message m = new Message();
Bundle b = new Bundle();
b.putString("sizeKEY", "size " + _count);
m.setData(b);
_handler.sendMessage(m);
And I need to access outer objects right here:
new Handler(){
@Override
public void dispatchMessage(Message msg) {
super.dispatchMessage(msg);
// get access to, say, Activity property or method here
}
}
How is it done ?
you can use
<MyOuterClass>.thisto access the outer lying object (cause you are in an inner class).If your outer class has the name “MyActivity” (just an example) and you want to access its extras then it’d be:
See
http://www.java-forums.org/java-tip/6296-inner-class-accessing-outer-class.html