How to check in an (Android) async task if the activity it was called from was finished?

72 views Asked by At

I have an android APP which has some async threads running on an activity. In these threads an alert dialogue box is called under certain conditions.

However if I leave the activity and the alert dialogue tries to open, the APP will crash as the context is no longer valid for display. (NOTE: I don't want the dialogue box to display anyway when the user has moved on/back to another activity).

Is there a way for me to check if the current activity was the activity that called the async task?

This way I can have an if(sameActivity){ alert.show(); }else{//do nothing}

1

There are 1 answers

0
TetianaDev On BEST ANSWER

Try this:

if (mContext instanceof MyActivity) {

    alert.show();
}else{//do nothing}

where mContext - your current context, MyActivity - your activity that called the async task