Activities in an android application arranged in a task back stack.When we use a single task launch mode for an activity then it can only belong to one task throughout all tasks in the app.It's vague to me why an application may or should has multiple tasks?
I saw Understand Tasks and Back Stack but it doesn't answer my question.
Take for example the Native Google Dialer. There the Incall Screen is opened in another task. That is how you can be in a call, but at the same time you can browse your contacts. Like almost they are different apps :) Otherwise if the Contacts Activity was in the back stack how would you do that?
Related
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.
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.
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
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.
I am trying to understand the need for task affinity in Android. I searched other SO answers as well.
I am particularly interested in the combination of a singleTask launch mode and task affinity.
For singleTask launch mode (OR Intent with NEW_TASK flag), system takes one of 3 actions:
If activity exists, it will resume it
If activity doesn't exist, it will look for a task with matching affinity to add the activity
Without finding a matching task, system will create a new task with this activity as root
I kind of understand the need to the first case from this answer. It talks about having same activity in multiple states, and how it can create inconsistency problems in user experience.
What I am more puzzled about is the second case - why does the system need to find an existing task with the same affinity ? What kind of use case does this fulfill and would break if this feature is disallowed ? Why is it necessary ?
From this link:
... if the intent passed to startActivity() contains the
FLAG_ACTIVITY_NEW_TASK flag, the system looks for a different task to
house the new activity. Often, it's a new task. However, it doesn't have to be.
If there's already an existing task with the same affinity as the new activity, the
activity is launched into that task. If not, it begins a new task.
The same link also talks about task reparenting. This is another feature
I cannot understand. The link does give an example of a weather/travel app:
... suppose that an activity that reports weather conditions in selected cities
is defined as part of a travel application. It has the same affinity as other
activities in the same application (the default application affinity) and it allows
re-parenting with this attribute. When one of your activities starts the weather
reporter activity, it initially belongs to the same task as your activity.
However, when the travel application's task comes to the foreground, the weather
reporter activity is reassigned to that task and displayed within it.
My question for this feature is similar. I'm not able to tell whether this feature is to fulfill some necessary user experience requirement OR is just a fancy add-on to tasks ? Why do activities need to be re-parented across tasks ?
Can someone please help me answer the above two questions ?
I'll take a stab at this!
I believe Android/Google's intentions were to have a seamless interaction for the user.
So when I read about affinity what came to mind were URLs, emails, documents and such. Does it make sense to ask the user what application to open especially if they already have one of the applications open that can handle the intent.
Same goes for re-parenting. The task opens another application but what happens when the user is finished with that task and wants to go back to the original application? In the user's perspective it is one experience regardless of how many applications are needed to meet that experience.
(I swear I read about this in a Material Design doc from Android....)
I am trying to understand conceptually what a task is in Android. So is it correct that a task can have multiple activities and can run one activity at a time? But is a task like a unix process? And each activity can be thought of as a thread within the process? Looking to clarify my somewhat weak understanding.
A task, unlike a Unix process, is basically an UI concept.
From the documentation - Tasks and Back Stacks
A task is a collection of activities that users interact with when
performing a certain job. The activities are arranged in a stack (the
"back stack"), in the order in which each activity is opened.
The activities that make up a task can belong to different applications (and hence, to different processes). That's why inter-activity communication in Android is done via Intents (basically, messaging objects than can be serialized) and not via direct method calls -- even when the called activity is part of the same application as the caller.
A user can switch freely between tasks (with the "Recents" button) but cannot move arbitrarily between the activities that make up a task. Normally they can only go one step back, via the "Back" button.
You should take a look at the documentation -- it's quite well explained there.
An Android application maps to a Unix process (more or less, since an app can run more than one process if it so desires) . But there is not a one-to-one mapping with tasks -- a task can contain activities from multiple applications, and a single application can have activities in multiple tasks.
Tasks and Back Stack is the definitive resource for understanding the mechanism involving tasks and their interaction with the Back button. In an early paragraph:
A task is a collection of activities
that users interact with when
performing a certain job. The
activities are arranged in a stack
(the "back stack"), in the order in
which each activity is opened.
Tasks are then explained in terms of the home screen and how it launches processes. This makes sense. I'm curious, however: What should determine the use of multiple tasks in "user" applications?
I'm interested in a intuitive understanding or heuristic guiding task usage rather than simply trying to achieve specific orderings of activities on the back stack.
A task is a group of components that work coherently together to fulfill a purpose for the user (not necessarily a very specific purpose, but a purpose nonetheless). It's what the user sees as an application.
A music player exists to allow the user to manage and play songs. It may include various activities that display album/artist/song metadata, control playback, organize playlists, etc. It may also include services that implement the playback and that watch for new songs. The user doesn't know what an activity and a service are; he or she only knows that this series of screens lets him or her manage and play songs.
The activities in a task don't need to belong to the same 'application' from a development standpoint. If the music player task allows users to link an image to a song, then it might launch an image gallery activity or the camera activity. Since the new activity is still working toward the purpose of managing songs, it's still part of the same task.
On the other hand, if the user completely breaks out of the task's purpose, it might be time to start a different task. For example, if you launch a web browser to view the artist's website, the user is now doing something different. The user probably doesn't associate web browsing with managing and playing songs, so this should probably be a different task.
I think a task in that given context is sort of a workflow. Take email. Writing a new email may consist of
first filling in recipients and then in the next activity selecting recipients and clicking on send finally sends the email off.
Another task could be selecting multiple emails and then deleting all in one go.
In practice what you often find is an AsyncTask which is basically doing work in background which otherwise would block the UI thread and make the app non-responsive.
I followed some tutorials but got confused with "Activity stack" and "Task".
Because both starts when a new activity is created.
Activity stack keeps a navigation history of activities, and Task is a sequence of activities.
Is this is only difference that Activity stack made up of one or more task(S)?
Give some example please.
Activities and Tasks
As noted earlier, one Activity can start another, including one defined in a different Application. Suppose, for example, that you'd like to let users display a street map of some location. There's already an activity that can do that, so all your activity needs to do is put together an Intent object with the required information and pass it to startActivity(). The map viewer will display the map. When the user hits the BACK key, your activity will reappear on screen.
To the user, it will seem as if the map viewer is part of the same application as your activity, even though it's defined in another application and runs in that application's process. Android maintains this user experience by keeping both activities in the same task. Simply put, a task is what the user experiences as an "application". It's a group of related activities, arranged in a stack.
Task = Application = Set of activities.
A task is not an application. The former is a set of Activities that the user has visited, while the latter is a collection of Android components (Activities, Services, ContentProviders and BroadcastReceivers) that are declared in an application's manifest.
The Activities of a given task can come from other applications as well as the current application. Taken together, these Activities represent the "path" that a user has taken to accomplish some objective. They are stored in the task's back stack in LIFO order; each task has its own back stack.
Task management, either through attributes such as launchMode, taskAffinity, etc, and/or intent flags, allows us to control the relationship between tasks and Activities.
For more information, please see: https://developer.android.com/guide/components/activities/tasks-and-back-stack