AndroIdiots Podcast 16: Android Process LifeCycle

Anupam Singh
AndroIDIOTS
Published in
4 min readSep 9, 2019

--

Android Platform has come a long way since its launch, from Cupcake to Pie. We had lots of deserts, product features like Notification Centre, Material Design, Dark-theme, capabilities like Google Assistant, Video Casting, Wireless charging etc.

But like any good software system the core always remains intact, so today we try to answer how the System designers went about designing android system, what are the design considerations that they kept in mind?

How system treats & interacts with the apps that we developers make? We deep dive into entire android process lifecycle with Deepak Garg and try to understand the same.

Show Notes :

Design Considerations while designing Android :

  1. In Background app in Mobile Vs Closed App on Desktop systems.
  2. No Swap Space in Android
  3. Frequent Application Switching
  4. “All Applications are created equal” philosophy.

The Android operating system is a multi-user Linux system in which each application is a different user. Android assigns a unique user ID (UID) to each Android application and runs it in its own process.

A process is created for an application specifically when some part of its code is run and the process keeps running until it’s no longer needed and system claims its memory to be used by for other applications.

Each Process has its own VM hence every app runs in isolation with other apps, this design also helps in securing apps to be infected by malicious apps.

ART starts the first VM which in turns calls the main method of original zygote process which then preloads all shared classes and resources to the memory.

Zygote: Every process in android is forked from Original Zygote Process. Original Zygote provides a template for all new mini zygote process and a VM is also assigned to every mini zygote process and the application is bound to the thread of this process.

Software Component Containers: Once loaded, they hold the activity, services, content providers component of the application, and the map of these preloaded classes is shared when an application is launched, dramatically reducing load time of application.

Activity Thread : The entry point for an activity, it manages the execution of main thread in an application process, handles broadcasts. and whatever is triggered from main method is called MAIN UI thread

When any application is launched the Zygote is bootstrapped, classes are loaded into the JVM, and the JVM to locates the java public static void "main" method and invoke it in Activity Thread.

And once main method is invoked it does 3 main things

1. Prepares the main Looper : Looper.prepareMainLooper method is called and this marks the current random thread as the applications main thread

2. Sets up a Handler : There is a private inner class called H that extends the Handler class.Which is initialised to sever as the main handler, H Handler contains all the definitions that an application can be in like LAUNCH_ACTIVITY, PAUSE_ACTIVITY, BIND_SERVICE etc.

This handler handles over 50 case statements. Resuming an activity, pausing it, starting it, binding services, handling receivers, flagging low memory, or trimMemory warnings all are handled here.

3. Calls the Looper.loop() method on the main thread: In order for work to take place on the main thread, the main method calls the Looper’s loop method on the main thread. This begins the execution of messages on the Loopers message queue. Now the main thread is up and running and can begin processing tasks from the queue.

The handler in ActivityThread class is responsible for handling every message that’s received in its onHandleMessage method. Resuming an activity, pausing it, starting it, binding services, handling receivers, flagging low memory, or trimMemory warnings all are handled here.

Even RELAUNCH_ACTIVITY is one of these cases. handleRelaunchActivity() is invoked which internally calls handleRelaunchActivityInner() which calls performLaunchActivity() which returns an Activity object. This Activity object already has crucial information like: Instrumentation, Context, Component and Intent information to the Activity.

The final step is when Instrumentation class method `callActivityOnCreate()` is invoked, which calls `performCreate()` method of the Activity class that calls Activity’s OnCreate method.

At this point, your Activity has been loaded with tonnes of useful variables and methods that can be used to create your awesome Android App! This is all thanks to the ActivityThread, some smart thinking behind the Handler and Looper framework.

Activity Creation LifeCycle

We are inviting Android Developers to contribute back to the community via AndroIDIOTS. If you are interested in recording a podcast or writing tech articles, we would love to have you on board. Here is a small form you can fill so we can get back to you.

Follow us on twitter for regular updates and feel free to DM the hosts (Anupam & Tushar) for any queries.

Cheers!!

--

--