0

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

4

2 に答える 2

1

Why can't you use a state variable in the onStart() and onStop() method to maintain the state of the Activity?

public class MyActivity {
    private boolean displayAlertOnStart = false;
    private boolean activityInAStartedState = false;

    protected void onStart(){
        super.onStart();
        activityInAStartedState = true;

        if (displayAlertOnStart){
            displayAlert();
            displayAlertOnStart = false;
        }
    }

    public void onStop(){
       super.onStop();
       activityInAStartedState = false;
    }


    private void handleTaskCallback() {
        if (activityInAStartedState) 
           displayAlert();
        else
           displayAlertOnStart = true;
      }    
}
于 2012-05-04T16:53:56.073 に答える
0

As far as I know, there's no method that lets you know if an activity is visible.

One way around this would be keeping a variable in your Application class which signifies if this Activity is on top. To implement this, you'll need to manage it in your Activity's onPause() and onResume() methods. Create a variable, something like private boolean myActivityOnTop = false with static getter and setter methods. In onPause(), set this variable to false, and make it true again in onResume. Then replace isActivityOnTop() with the getter method. You could do the same thing if you want to know which of a number of Activities is on top by making this an int, string, or enum and setting it whenever any of the activities' onResume is called.

Also, just a tip, whenever an Activity is pushed to the background, there is no guarantee that Android will not destroy it. It is possible that you set displayAlertOnStart to true, but the Activity is then destroyed. When it restarts upon being brought back to the front, the Activity will be recreated and will not know to display the Dialog. Any variables that you'll want to maintain after starting a new activity should be Bundled in onSaveInstanceState() and re-initialized in onCreate() or onRestoreInstanceState().

于 2012-05-04T17:09:11.413 に答える