I have the following Android scenario:
my activity launches an asynchronous task to perform background activity (AsyncTask
class), and then searches for accounts. If no account is present, UI to create a new account is invoked via the AbstractAuthenticator
while the previous AsyncTask still runs.
The task will eventually complete and run onPostExecute on the main thread of my previous activity.
My problem is the following: if the task completes when my activity is on top, an AlertDialog
is correctly displayed; instead, if the task completes while the user is entering username/password for the new account, I get an InvocationTargetException
when trying to show the alert.
Actually, the AlertDialog must be shown only when activity is active. I can modify my code and take advantage of onStart
method with the following pseudo code:
public class MyActivity {
private boolean displayAlertOnStart = false;
protected void onStart(){
super.onStart();
if (displayAlertOnStart){
displayAlert();
displayAlertOnStart = false;
}
}
private void handleTaskCallback() {
if (activityIsOnTop()) //How do I get this???
displayAlert();
else
displayAlertOnStart = true;
}
I would like to programmatically know if the current activity is "on top" or another activity is foreground. This way I'll do my logic when running the onStart the next time.
Any other (and simpler) solutions welcome. SDK 7.
Thank you