Varun Sharma
Varun Sharma

Reputation: 142

Difference between Process,Activity,Threads and Tasks in Android

What is the difference between all the above? I found various posts which were helpful but also quite confusing. According to my understanding in short this is what I came upto:

Upvotes: 3

Views: 4612

Answers (3)

Chrys Thorsen
Chrys Thorsen

Reputation: 1

I know this is old, but you can also say that a thread is the smallest unit of execution of code. Threads are scheduled to run on the CPU. A process can have one or more threads.

Upvotes: -1

Thomas Leyk
Thomas Leyk

Reputation: 306

You should differentiate between Processes & Threads vs. Activities vs. Task. They aren't even really in the same category.

Let's start with the simplest one, Task's. Assuming you are not talking abouy any actual class, i.e. TimerTask, the basic concept of a Task is the following. When a user starts your app for the first time, a new Tasks is created. You can see this by pressing the "OverviewButton", represented by a Square for the software buttons. (on Android 5.0 an higher) A Task will not get disposed of, unless the User actually removes(swipes left/right) it from the Overview screen. So a Task is really just a high-level abstraction for the user. Like you alluded to, a Tasks has an Activity backstack, which is just a normal stack that is used to keep track of the "history" for the user. For example, your App is launched, your MainActivity will be at the bottom of the stack, the User enters some values and then goes on to a new Activity. Now this new Activity is above the previous one, and the user can press the "back button" at -hopefully- any time to get back to the previous activity.

Now for Processes &Thread's, a Processes under Android is very similar to a linux process, your app will usually only be working within one single process. A process gets assigned a certain part of the memory by the OS, if you're familiar with languages like C, attempting to acess memory that does not belong to your process wil cause a "segmentation fault".

Like you said, a process may have any number of Threads, assuming the OS can manage the required Overhead. A process will at least have one Thread, under android this is called Main-Thread or UI-Thread. Threads, very basically, allow you to do some work in parallel. You will most likely need to make use of them, for example when performing network operations.

Now for Activities, they have no direct relationship to multithreading. The currently "active Activity" is the one that is run on the UI-Thread. So all of its callbacks will be run on the UI-Thread, unless specifically documented not to. An Activity is an abstraction used by the android framework, it exists at a fundamentally different level than a Processes & Thread's. You can call a method defined in a Activity, from any Thread you want.

Upvotes: 9

Victor Ijishakin
Victor Ijishakin

Reputation: 158

A really nice question, from my little experience with android development, I'd like to contribute. Let's start from..

Processes

Ever opened task manager on windows to see open apps? Those are processes. On android, when an app is launched, a new process is opened and allocation of memory etc is given. The activity classes,imports and threads all make a process in the Android system. sometimes you see an error message when an app crashes "unfortunately com.android.bakerapp has stopped." This means an error causes the whole process of threads, imports, activities to close. So basically processes are parts of an app or an app in general that's running.

Activity

An activity is the heart and soul of all android apps, all Threads, preferences, views and layouts are opened by android activity class. It is the container object that holds views, passes information around and runs threads too. Activities communicate with each other through intents, objects in the class extends and methods. Activity is the piece of code that creates and communicates UI and everything a user sees and uses. It is used to create threads. Which is discussed below.

Threads

This one is easy, a thread is basically a process to get something done, it lives and dies after its work. Imagine you have an activity with a view of picture on the screen and you want to automatically set your apps theme color to the most common color on the picture using a library.

The best method to do this without the user knowing and also confusing the main thread responsible for loading the picture into a view from a website is to open a thread using an Asynchronous task (something that runs in background) is an example of a thread.

So a thread is basically a life cycle of a task to be done, it can be continuous (Main activity views and list views) or short (Find a dominant color in a picture) or fun and multitasking (downloading a picture from a group chat while at thesame time chatting with your girlfriend on WhatsApp).

Threads are the most essential part of all activities and processes and can send,receive and process data.

Activities cannot work without threads because the setContentview and UI itself is just another thread, you can have multiple threads in one activity.

Happy coding!

https://developer.android.com/guide/components/processes-and-threads.html

Upvotes: 2

Related Questions