Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to launch an activity from a non-ui thread?

Tags:

Suppose my activity ui-thread spawns a separate thread at some point. Is it safe for that new thread to fire off an Intent for launching a new activity?

Thanks

like image 367
Mark Avatar asked Jul 21 '10 02:07

Mark


People also ask

Why should you avoid to run non-UI code on the main thread?

If you put long running work on the UI thread, you can get ANR errors. If you have multiple threads and put long running work on the non-UI threads, those non-UI threads can't inform the user of what is happening.

What is the UI thread and when should you use it?

User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.

Is the UI the main thread?

UI Thread and Main Thread are same only in Android. The Main thread, that is responsible for handling the UI events like Draw, Listen and receive the UI events. Ans also it is responsible for interact with running components of the UI toolkit for the corresponding application that belongs to.

Can we update UI from background thread?

Android Thread Updating the UI from a Background Thread The solution is to use the runOnUiThread() method, as it allows you to initiate code execution on the UI thread from a background Thread.


1 Answers

I could be wrong, but I think startActivity() internally executes in the UI thread. So yes, it should be safe.

If you wanted to be 100% sure, invoke your startActivity() in your activity's runOnUiThread() as a Runnable.

like image 131
Andy Zhang Avatar answered Oct 08 '22 14:10

Andy Zhang