Examples of various launch modes of Activity? - android

So I have read that there are four launch modes for an Activity in Android as follows
1) standard : create a new instance of an activity every single time.
2) single top : same as standard except that if the activity is at the top of the stack, then the same instance will be used.
3) single task : a new task will be created whenever this activity is created. Also only one instance will be available among all the tasks.
4) single instance : the activity will be created in a new task, and that task will contain only that activity. Also only 1 instance of that activity will be available for all the tasks.
Till now, I did not use any of the launch modes in my activities. So by default, i was using standard launch mode.
I was wandering what are the various kinds of activities where other launch modes will be used.
Do the Android applications (like search , map) have activities which uses other launch modes as well ?

There are some uses here:
http://www.intridea.com/blog/2011/6/16/android-understanding-activity-launchmode
I've used single top once for an activity that was called by two async tasks for Bluetooth and wifi that basically launched as soon as they received a response message from the server. Sometimes two messages would arrive and double launch the activity within 1s of eachother and that's how I found out about launch modes.
As far as the native android apps go, I'd say it's hard to tell what is going on under the hood for launch modes but I'd question if this piece of information would even matter. You know what they do now, so use em when it's applicable :D

Generally speaking there are just two common modes: Standard and Single Top, standard allows you to have multiple instances of the same activity, for example somebodys profile will be a great idea for standard lunch mode, as there could be multiple profiles in your app.
But if you are implementing a lobby activity, you probably want to set it as single top, as you don't want to launch new lobby activity each time you send someone back to the lobby.
And there are another two modes: Single task and Single Instance, both of them require that they will be the ROOT of the stack and the difference between them is that Single Instance also don't allow any other activities in the stack.
You will probably use Single Instance for some security app, for example processing credit card details.
And Single Task can be used for updating some important user information, so you want to make sure you always start clean and up to date.

Related

Tasks in 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.

How to choose correct flags and attributes

I've been reading quit a lot on the subject and I'm still confused. In order to apply the correct flags and attributes to my application I need confirmation on the mental picture I have right now.
Can / will someone please conform or falsify following statement:
1.
A device runs multiple applications; true.
2.
An application contains multiple activities; true.
3.
An application holds one and only one stack (back-stack or task); true.
4.
An activity can (but not must) run in multiple applications, so the activitu in question can be part of multiple but very different stacks; true.
5.
Stack, back-stack and task are just different words for one and the same thing; true.
If false please explaine.
Everything you list is more or less true except for your understanding of "application". This applies to statements 1, 2 and 3 and 4.
In Android, an application represents a package of components (ie: activities, broadcast receivers, services and content providers) that are stored together. During runtime, the concept of application is pretty loose, and only relevant regarding security and permissions (which components can access which other components and data). Android doesn't run applications.
When a user starts an application, what really happens is that Android launches a single activity into (usually) a new task or an existing task (Note: this is a simplification, there are certain cases where a new activity isn't created).
Android runs components, and the components run inside a Virutal Machine (VM) inside a process. A process is an OS entity.
In Android, a task is a sequence of activities that are started and are bound together. A task may contain activities from multiple applications, or may contain activities all from the same application. The activities within a task are in a specific sequence and this is referred to as the task stack, activity stack, back stack or just stack. These activities can be running all in the same process, or they can be running in different processes. It is important to understand that a task and a process and an application are very different things.

How to have overall data control in an Android application?

I'm starting to work with Android, and as far as I have read, the main structure of an app is a group of more or less independent Activities where one is the main, and from there you launch one or another.
My problem is that some of those activities spend some time when they are created to generate some data, that is lost when the activity ends because of the paradigm of Android.
Also, I want to have some overall control of some parts of my program. For example, I activate a sensorListener in one activity, and I want to keep it working after I end that activity (by pressing "back" or launching another activity).
Is it possible to have some common structure to all the activities where I can place reusable data?
Also, I whould like my app to do something periodically , no matter what activity is working at the moment.
Do you know if there is a "well designed" way to program this overall data structure and periodic tasks?
You can use your "Application" class to have an entrypoint. This class won't get dealocated, you can save references in there, however, this is not a good style of programming but I have seen it a lot. If it's possible, a better way is to use threads, e.g. "AsyncTask" class. Here you can perform your operations and populate the activity on the fly.
As L7 pointed out you may use "service" as a long running background process for your sensor, this is also the recommended way of android.

Difference between task and process in Android

I'm a bit confused about the difference between a task and a process in Android.
If I understand correctly a task is just a stack of activities. From what I read so far I think a task could look like this:
| Activity A running in Process P1 |
| Activity B running in Process P2 |
| Activity C running in Process P3 |
So basically activities from different processes can be contained in the same stack. Am I correct?
Another question: What is the real meaning of "application context"? The process or the task?
And final question: The application class (which is basically a singleton) represents the process or the task?
Everything I've ever learned about it, I've learned on this page.
Edit: I also just stumbled upon Activity and Task Design Guidelines. It looks to cover the exact topic you asked about. I learned a lot :)
So basically activities from different
processes can be contained in the same
stack. Am I correct?
Based on my understanding, you are correct. My grasp is that Processes are the units of actual execution while Tasks are about association to get things done. As an example from the aforementioned page, if you create an intent that opens a webpage, the Activity that it creates is created on the web browsers process but is associated with your applications Task. A task, therefore, becomes a virtual stack of Activities running on different processes depending on the application that provided the Activity.
Another question: What is the real
meaning of "application context"? The
process or the task?
This is a good question. Based on reading the page above, my understanding is that an Applications context is associated with the process. I'm basing that on the interpretation of this line from that page, but there may be other info:
Normally, a new instance of an
activity is launched into the process
of the application that defined it, so
all instances of the activity run in
the same process
And final question: The application
class (which is basically a singleton)
represents the process or the task?
With the same interpretation as above, my guess as to why an Application object represents a Singleton is because all of your applications activities get run on a single process and that process is tied to the Application. I don't know that this is a design point, but it appears to be, at the least, a consequence of the current design.
Edit: There are some caveats to this. It appears that your application can be spread across multiple processes so, my guess is that the Application Object and context act as a mechanism for tethering all the processes together. I'm pretty sure your mental model already allowed for this, assuming the processes were from different applications, so its only a small difference to allow it inside a single process.
The manifest element has the attribute android:process with the description as follows:
The name of the
process in which the activity should
run. Normally, all components of an
application run in the default process
created for the application. It has
the same name as the application
package. The element's
process attribute can set a different
default for all components. But each
component can override the default,
allowing you to spread your
application across multiple processes.
If the name assigned to this attribute
begins with a colon (':'), a new
process, private to the application,
is created when it's needed and the
activity runs in that process. If the
process name begins with a lowercase
character, the activity will run in a
global process of that name, provided
that it has permission to do so. This
allows components in different
applications to share a process,
reducing resource usage.
Process:
When an application component starts and the application does not have any other components running, the Android system starts a new Linux process for the application with a single thread of execution. By default, all components of the same application run in the same process and thread (called the "main" thread).Every application runs in its own process and all components of the application run in that process, by default.
for detail process: http://developer.android.com/guide/components/processes-and-threads.html
Tasks:
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.
An activity can even start activities that exist in other applications on the device. For example, if your application wants to send an email, you can define an intent to perform a "send" action and include some data, such as an email address and a message. An activity from another application that declares itself to handle this kind of intent then opens. In this case, the intent is to send an email, so an email application's "compose" activity starts (if multiple activities support the same intent, then the system lets the user select which one to use). When the email is sent, your activity resumes and it seems as if the email activity was part of your application. Even though the activities may be from different applications, Android maintains this seamless user experience by keeping both activities in the same task.
for detail task-http://developer.android.com/guide/components/tasks-and-back-stack.html
An important note from Android Developer :
A common misunderstanding about Android multitasking is the difference
between a process and an application. In Android these are not tightly
coupled entities: applications may seem present to the user without an
actual process currently running the app; multiple applications may
share processes, or one application may make use of multiple processes
depending on its needs; the process(es) of an application may be kept
around by Android even when that application is not actively doing
something.

Task and Activity stack : what is difference between both.

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

Categories

Resources