Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 881
Satyo Isworo Avatar asked Jun 27 '13 01:06

Satyo Isworo


2 Answers

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.

like image 64
Shobhit Puri Avatar answered Sep 20 '22 01:09

Shobhit Puri


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

like image 33
Squonk Avatar answered Sep 19 '22 01:09

Squonk