3

I have an app that displays a splash screen. The splash screen activity creates a new Runnable which simply sleeps for 1 second and and then launches the main activity:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splash);

    UKMPGDataProvider.init(this.getApplicationContext(), Constants.DATABASE_NAME);

    Thread splashThread = new Thread() {
        @Override
        public void run() {
            try {
                sleep(ONE_SECOND_IN_MILLIS);
            } catch (InterruptedException e) {
            } finally {
                Intent intent = new Intent(SplashScreen.this, MainScreen.class);
                finish();
                startActivity(intent);
            }
        }
    };
    splashThread.start();
}

Is it OK to launch the main activity (and therefore the whole app except for the splash screen) on this new thread?

We hear a lot about the "UI thread" in Android. Does this new thread now become the UI thread, or is the UI thread special in some way?

Andrey Ermakov
  • 3,298
  • 1
  • 25
  • 46
barry
  • 4,037
  • 6
  • 41
  • 68

2 Answers2

1

Yes, that's fine. startActivity(intent) asks the system to launch your main Activity. You're not actually loading it yourself in the thread you call that from.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
0

Basically it's a single-thread model where only one thread can modify the UI because the Android UI toolkit is not thread-safe.

It's same in Blackberry as well. See Why are most UI frameworks single-threaded?

Community
  • 1
  • 1
Dheeresh Singh
  • 15,643
  • 3
  • 38
  • 36
  • Ok, but the question was whether it was ok to launch an Activity from a non ui thread, or if this is what was happening. – barry Jun 15 '12 at 15:26