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
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.
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With