Satyo Isworo
Satyo Isworo

Reputation: 61

what is the difference between activity and thread on android app?

I am confused about the difference between activity and thread on android application. So is an activity like an independent thread? If so can multiple activities run simultaneously in a multi-threaded application?

Thank you

Upvotes: 6

Views: 6537

Answers (7)

user3219477
user3219477

Reputation: 144

Activity represent single screen and it has UI and other hand thread doesn't have UI and it is used to performing a background operation

Upvotes: 0

Mehroz Munir
Mehroz Munir

Reputation: 2356

Activity is any ui that user sees when using the app , a thread is a place where your tasks are running .. there is always one thread that is called main thread to run your all ui and processes in application , if you want to increase the speed of execution you need to create more threads so your main thread does not disturbed and your heavy tasks run on the background.

Upvotes: 0

Victor Pinto
Victor Pinto

Reputation: 187

An acitivity is an user interface while a Thread is a process worker that executes some code. Android does have ONE main thread who controls all the User Interface(UI), so if you do not specify a different thread that runs your activity, the Main Thread will run your entire UI.

Upvotes: 0

posha
posha

Reputation: 901

AS android.h have mentioned All the UI Objects, activities run on the Main Thread. But Still u can use async task to read data from web services in another thread which will help you to not to do the task in the background. Hope my answer helped you.

Upvotes: 0

Squonk
Squonk

Reputation: 48871

So is an activity is an independent thread?

Yes and no. An Android app with one Activity will have a single process and single thread but if there are multiple app components they will normally all use the same thread (except for certain Android classes which use their own threads to do work).

Please read the following...

Processes and Threads

if so can multiple activities run simultaneously being a multi-threaded application?

An Activity is only considered to be "running" when it is completely visible. For example, when a popup (e.g., Dialog etc) appears, the underlying Activity is still partially visible but will be in a "paused" state. If another Activity is started and completely hides the previous one (whether it's part of your own app or an external app), the previous Activity will go into a "stopped" state and may even be destroyed.

Basically an Android Activity is not a work-horse to allow multi-tasking in a multi-threaded environment. An Activity is basically a UI framework to provide buttons, text views, images etc and to allow user interaction.

See also...

Application Fundamentals

...and also look at the Activity lifecycle diagram here...

Activity Lifecycle

Upvotes: 5

Shobhit Puri
Shobhit Puri

Reputation: 26007

I beleive you might have read the documentation of What is an Activity? before. If not, then please do. Here you may read more about process and threads in android. Now, answering your question:

Is an activity is an independent thread?

Each activity is not an independent thread. As @android.h mentioned in the comments, all activities run on the same UI thread.

Can multiple activities run simultaneously being a multi-threaded application?

As said above all the Activities, Services, ContentProviders, BroadcastReceivers etc run on UI thread. That being said, you can start multiple threads from within an activity itself. So, you application can use multiple threads but running multiple activities does not make it multi threaded.

Taking about multiple activities, you might read Tasks and Back Stack document. It highlights about the concept of multiple activities:

An application usually consists of multiple activities that are loosely bound to each other. Typically, one activity in an application is specified as the "main" activity, which is presented to the user when launching the application for the first time. Each activity can then start another activity in order to perform different actions. Each time a new activity starts, the previous activity is stopped, but the system preserves the activity in a stack (the "back stack"). When a new activity starts, it is pushed onto the back stack and takes user focus. The back stack abides to the basic "last in, first out" stack mechanism, so, when the user is done with the current activity and presses the Back button, it is popped from the stack (and destroyed) and the previous activity resumes . So here is how multiple activity thing works.

Hope this made your concept a bit more clear.

Upvotes: 7

Fernando Camargo
Fernando Camargo

Reputation: 3110

You should understand the concept of UI Thread. Basically, there's a main thread (the one that call your callback methods of the activity) and you can start other threads. The threads you start can't update the UI, just the UI thread.

See more here: What is the Android UiThread (UI thread)

Upvotes: 1

Related Questions