I have codes below that will execute a callback from another thread when I finish the activity. So how to avoid to call the callback or the codes in the callback when the activity has been finished?
public static interface Callback{
public void onCallback();
}
class ActivityA {
TextView tv = ...;
Handler handler = ...;
public void onClick(View v) {
Business.callThread(new Callback() {
@Override
public void onCallback() {
handler.post(new Runnable(){
public void run(){
tv.setText("xxxx");
}
});
}
});
}
}
class Business {
public static void callThread(final Callback listener) {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000); //sleep 5s;
} catch (InterruptedException e) {
e.printStackTrace();
}
listener.onCallback();
}
}).start();
}
}
Garbage collector counts references to objects. However, there are couple of reference types. Useful in your case is
WeakReference
:Create runnable as a class with constructor:
then call listener like:
callThread
method implementation: