Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - onPostCreate() code firing before onCreate() code

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", "[email protected]").toString();
  if (email.equals("[email protected]")) 
  {
    //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()?

like image 471
JMax2012 Avatar asked Dec 19 '25 15:12

JMax2012


1 Answers

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.

like image 175
Geobits Avatar answered Dec 21 '25 05:12

Geobits



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!