Search code examples
androidmultithreadingandroid-activityprocesstask

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:

  • Threads are tasks that share same resources
  • Processes are tasks which have independent rersources. A process can have multiple threads.
  • Tasks are the instructions being executed
  • Now this is where I get confused. How is an activity related to all these three in android. Activity can have multiple tasks so it must be something like a process. But then what is the difference between activity and process. Moreover I read somewhere that tasks are stack of activities. It got me all confused. Ive also read that all activities run on UI thread which just make the distinction a little more confusing.

Solution

  • 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.