Thursday, February 5, 2015

Android beginner tutorial Part 54 Activities and processes

Today we will learn about Activities and processes in Android.

So far weve 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, theres 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 its 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 thats hosted by the process or the service thats 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 theres an Activity that partly covers another Activity (a dialog box, for example). This process is considered an important one and wont be destroyed as long as there are processes with lower priorities running.

The next process is a Service Process, it executes a Service and doesnt 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 doesnt 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 lets 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 theres 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:



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, youll see all the 3 states Ive 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.

Thats all for today.

Thanks for reading!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.