barry
barry

Reputation: 4147

Difference between UI thread and other threads

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?

Upvotes: 3

Views: 1083

Answers (2)

Dheeresh Singh
Dheeresh Singh

Reputation: 15701

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?

Upvotes: 0

Darshan Rivka Whittle
Darshan Rivka Whittle

Reputation: 34031

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.

Upvotes: 1

Related Questions