title: Android application startup sequence
body: <p>I am working on an app where I need to bind to a database via IBinder.  I planned on putting any framework initialization in my Application subclass since that kind of stuff isn't presentation related and I know that the Application will be instantiated once per app. </p>
  
  <pre><code> public class MyApplication extends MultiDexApplication {
     public void onCreate() {
        super.onCreate();
        // Bind to the db server
        Intent intent = new Intent(getBaseContext(), DB.BIND_ADDRESS);
          super.bindService(intent,
            new ServiceConnection() {
            @Override
            public void onServiceConnected(ComponentName name, 
                                           IBinder service) {
              DB.start(service);
            }
            @Override
            public void onServiceDisconnected(ComponentName name) {
              DB.shutdown();
            }
          },
          Context.BIND_AUTO_CREATE
        );
    }
  }
  </code></pre>
  
  <p>The problem that I run into is that the call back to my ServiceConnection.onServiceConnected() isn't performed until my activity has started and that's a problem since I need the database available for the activity to display the data.  I currently see this behavior.</p>
  
  <pre><code>myApplication.onCreate()
  myActivity.onCreate()
  myActivity.onStart()
  serviceConnection.onServiceConnected()
  </code></pre>
  
  <p>This is a problem since the database isn't yet connected when the onStart() is invoked on my activity.  </p>
  
  <p>I would rather not have to move this infrastructure initialization in the activities.  So I have two questions.</p>
  
  <ol>
  <li>Is there a way to block the application.onCreate to wait until
  the onServiceConnected() has been invoked?  This being single
  threaded I don't see how.</li>
  <li>Is there a way to tell android not to invoke any onStart() on any
  activities prior to my application having completed its
  initialization such that I have the following startup sequence</li>
  </ol>
  
  <p>This</p>
  
  <pre><code>myApplication.onCreate()
  serviceConnection.onServiceConnected()
  myActivity.onCreate()
  myActivity.onStart()
  </code></pre>
  
  <p>or</p>
  
  <pre><code>myApplication.onCreate()
  myActivity.onCreate()
  serviceConnection.onServiceConnected()
  myActivity.onStart()
  </code></pre>