title: How to run code on Application opens in Android?
body: <p>My Android app need the user to create an account to be able to use the app. The account info is stored in SQLite database. When the application starts I check if the user has an account, if not I show a sign up activity for the user. </p>
  
  <p>Now I get reports from users that they sometimes comes to the sign up activity even if they've already created an account. This happens when they've closed the application and reopen it again.</p>
  
  <p>This is the code I'm using and I need to figure out what the problem might be:</p>
  
  <pre><code>//MyApplication.java
  public class MyApplication extends Application {
      private DataBaseUtility dbu;
      public boolean hasAccount;  
  
      @Override
      public void onCreate() {
          super.onCreate();
  
          //Init sqlite database
          this.dbu = new DataBaseUtility(this);
  
          //This loads the account data from the database and returns true if the user has already created an account
          this.hasAccount = loadAccount();
      }
  
      public boolean loadAccount() {
          boolean loadedData = false;
  
          String query = "SELECT data FROM tblaccount WHERE tblaccount.deleted=0";
          Cursor cursor = this.dbu.getCursor(query);
          if (cursor != null) {
              while (cursor.moveToNext()) {
                  loadedData = true;
              }
              cursor.close();
          }
  
          return loadedData;
      }
  }
  
  //MainActivity.java
  public class MainActivity extends TabActivity {
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
  
          MyApplication application = (MyApplication)getApplication();
          if (!application.hasAccount) {
              //Take the user to the sign up activity
          }
  }
  </code></pre>
  
  <p>My idea is that maybe sometimes <code>MainActivity.onCreate()</code> runs before <code>MyApplication.onCreate()</code>.  Can that be the case?</p>