Android Activity Lifecycle Car Analogy

Aug 5, 2013 · Follow on Twitter and Mastodon

In most application frameworks, classes have a lifecycle that is used to do customizations at the proper time. Let’s look at the Android activity lifecycle compared to a car.

Image of an Android teacher

I saw this great analogy for describing the Android Activity lifecycle as a car. This can help us map where we should put your activity customizations.

protected void onCreate(...) {
    openGarageDoor();
    unlockCarAndGetIn();
    closeCarDoorAndPutOnSeatBelt();
    putKeyInIgnition();
}

protected void onStart() {
    startEngine();
    changeRadioStation();
    switchOnLightsIfNeeded();
    switchOnWipersIfNeeded();
}

protected void onResume() {
    applyFootbrake();
    releaseHandbrake();
    putCarInGear();
    drive();
}

protected void onPause() {
    putCarInNeutral();
    applyHandbrake();
}

protected void onStop() {
    switchEveryThingOff();
    turnOffEngine();
    removeSeatBeltAndGetOutOfCar();
    lockCar();
}

protected void onDestroy() {
    enterOfficeBuilding();
}

protected void onReachedGroceryStore(...) {
    Intent i = new Intent(
        ACTION_GET_GROCERIES, 
        ...,  
        this, 
        GroceryStoreActivity.class
    );
}

protected void onRestart() {
    unlockCarAndGetIn();
    closeDoorAndPutOnSeatBelt();
    putKeyInIgnition();
}