Tutorial details

Android beginner tutorial: Part 54 - Activities and processes | App Code for Sale | Preview

Android beginner tutorial: Part 54 - Activities and processes | Android Tutorial

Activities and processes in Android

Overview PAGE TOP

Today we will learn about Activities and processes in Android.

So far we've only included one Activity in all our previous examples. In reality, applications usually have multiple Activities. There are a few things we should know before proceeding to actually creating and working with multiple Activities.

Even though an application can have multiple Activities running at the same time, one of them is marked as "main". It is the one that is the first to load once the application is started.

It should also be noted that an application is capable of invoking not only the activities whithin that application, but also activities that belong to other applications.

When at least 1 component in an application is needed, the Android system launches a process that contains a single thread for running the code. By default, there's just one main thread for the process. It is, however, possible to add new threads, which we did in some of the previous tutorials (for example, when handling progress bars). It is recommended to keep processes that might freeze the application in separate threads. These are processes like downloading, calculating, uploading, etc. The Android system might want to stop a thread process if it's taking too much memory and that memory is needed for other more important processes.

Android chooses which processes to destroy based on their priorities. There are 5 different process priorities, from most important to least important - Foreground Process, Visible Process, Service Process, Background Process and Empty Process.

A process is considered a Foreground Process if: user is currently interacting with the activity that's hosted by the process or the service that's related to that activity, or if the process hosts a Service object that is running in the foreground (startForeground() has been called) or it is executing one of its lifecycle callbacks (will talk about these callbacks later), or if the process hosts a BroadcastReceiver object that is executing its onReceive() method.

There may be more than one foreground processes running at the same time and they are killed only in critical situations, when memory is so low that these processes are unable to continue to run.

A Visible Process is the one that has a component, which the user can still interact with, even though it is not focused, but still visible. This can happen if there's an Activity that partly covers another Activity (a dialog box, for example). This process is considered an important one and won't be destroyed as long as there are processes with lower priorities running.

The next process is a Service Process, it executes a Service and doesn't belong to any of the previously mentioned process types. Services are usually not attached to an interface and their purpose is to execute actions that the user needs to run in the background. An example of this is a music player that plays music in the background while the user does something else.

A Background Process is the one that is not visible to the user and does not impact their experience at all. There are usually many of these running at the same time, but they are stored in a list and ordered by the last time the user used the application. This way, the most recently used app will be killed last out of all the background processes.

An Empty Process is the one that doesn't hold any active application components and is just kept alive for caching purposes (in order to improve the startup time of some components) and has the lowest priority out of all processes.

Now let's talk about Activity states.

There are 3 possible states: active (running), paused and stopped. An active Activity is the one that the user is currently interacting with. A paused one is the one that has lost its focus, but is still visible to the user. It is considered stopped if it is completely covered by another activity. It can be killed by the system if there's memory needed for more important processes.

Earlier in this tutorial I mentioned lifecycle callbacks. They are protected methods that are called when the application changes its state. There is a nice scheme on the official Android dev site which I really like and am going to shamelessly present here:

sleep.png

As you can see, there are 7 callback functions in total. Out of them all, only one is necessary in each Activity - onCreate() mehod, which we frequently used in the previous tutorials.

If you carefully examine the scheme, you'll see all the 3 states I've mentioned - running (labeled Resumed in the scheme), paused and stopped. All the callback functions are called during the transition from one state to another and are quite useful. We will focus on them more in the future tutorials.

That's all for today.

Thanks for reading!

Reference PAGE TOP

http://kirill-poletaev.blogspot.it/2013/01/android-beginner-tutorial-part-54.html

0 Comments Leave a comment

Please login in order to leave a comment.

Newest first
!

Sign-in to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.