Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Application class does it run on UI thread or?

Sorry for being a newbie but i created a method in Application class in java, is it safe to run a method with complex to medium algorithm? is it going to be a hiccup in the UI's?

like image 883
david Avatar asked May 22 '15 07:05

david


People also ask

What is run on UI thread?

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.

Does Android service run on main thread?

Caution: A service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise. You should run any blocking operations on a separate thread within the service to avoid Application Not Responding (ANR) errors.

What is difference between UI thread and main thread?

Originally Answered: What is difference between UI thread and main thread in Android? UI thread is what render UI component/Views. Main thread is what which start the process/app. In Android UI thread is main thread.


3 Answers

Yes all application components from activity to broadcast receivers run on ui thread,only when you have to do some long fetching task or background execution or a network fetch do it in a separate thread using asynctask or intent service,so that it does nor slag down your ui screen.

like image 176
Srishti Roy Avatar answered Oct 17 '22 08:10

Srishti Roy


From Processes and Threads | Android Developers (emphasis mine):

When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread). If an application component starts and there already exists a process for that application (because another component from the application exists), then the component is started within that process and uses the same thread of execution. However, you can arrange for different components in your application to run in separate processes, and you can create additional threads for any process.

And:

The system does not create a separate thread for each instance of a component. All components that run in the same process are instantiated in the UI thread, and system calls to each component are dispatched from that thread. Consequently, methods that respond to system callbacks (such as onKeyDown() to report user actions or a lifecycle callback method) always run in the UI thread of the process.

So yes, methods like onCreate in your Application class will be called on the main (UI) thread.

There are only a few classes that do start asynchronously, like the IntentService for example.

like image 30
nhaarman Avatar answered Oct 17 '22 08:10

nhaarman


complex to medium algorithm

if it is complex, you should run it in an asynchronous way, using a Thread, an AsyncTask, an IntentService or whatever suits you better, but don't run it directly on the a subclass of Application/Activity/Fragment/Service or whatever runs on the UI Thread. It will slow down the start up of you application otherwise.

like image 42
Blackbelt Avatar answered Oct 17 '22 06:10

Blackbelt