3

I have two blocks of "validation" code that I need to execute in a specific order, but the code in the onPostCreate() event is firing before the code in the onCreate() event, and I suspect it may have something to do with the Preference Store.

Some pseudo-code may help explain:

onCreate()
{
  prefs = PreferenceManager.getDefaultSharedPreferences(this);
  email = prefs.getString("email", "noemail@noemail.com").toString();
  if (email.equals("noemail@noemail.com")) 
  {
    //user has not supplied email address, show alert dialog
    warning();
  }
}

The warning() method just builds an alert dialog box letting the user know that they need to enter an email address, and when they click "OK" it launches the Preferences activity so they can supply the email address.

onPostCreate()
{
  carrier = manager.getNetworkOperatorName();
  if(carrier.equals("SPRINT"))
  {
    //call web service to validate carrier compatibility
    //if web service returns "FALSE" call warning2();
  }
}

The warning2() method does the same thing as warning() - it builds an alert dialog to let the user know their carrier is not compatible.

Even though warning() is called in onCreate(), and warning2() is called in onPostCreate(), currently the app is throwing the warning2() dialog box before the warning() dialog box, and I don't understand why.

Is it because the second validation calls a web service and the first validation is checking the Preference store, and somehow the web service call is completing before the Preference store can be accessed?

How can I enforce that the first validation and warning() are dealt with before the second validation and warning2()?

JMax2012
  • 373
  • 2
  • 6
  • 24

1 Answers1

2

Are you sure it's calling the warning2 dialog first, or are you just seeing it first? Dialogs don't block the main activity, so it's probably calling warning first, but immediately after that, it calls warning2, which would cover the first dialog. So you would see warning2, and not see warning until it was dismissed, since it's layered directly below it.

Geobits
  • 22,218
  • 6
  • 59
  • 103
  • Ah, good point. I was assuming that if warning() was called first, that dialog would be displayed in a modal fashion so that the warning2() dialog couldn't be shown until the first one was dealt with. – JMax2012 Sep 11 '12 at 16:52
  • Thanks, that's what was happening. Warning2() was getting called in the right order, it was just layering over warning(). I should have caught that. Thanks again! – JMax2012 Sep 11 '12 at 16:59