mobile/app-lifecycle-why-your-phone-forgets-what-you-typed-when-you-switch-ap.md

App lifecycle: why your phone forgets what you typed when you switch apps

Mobile · 4 min read ·

You switch apps for a minute, and when you come back the message you were typing is gone. Nothing was broken: the phone needed memory, ended the app's process, and everything that lived only in memory went with it.

This short read explains how Android and iOS treat apps as guests, what stopped, background and suspended mean, and why you can't save at the moment an app is ended. Use the pretend phone to lose a draft on purpose, then save it properly.

A phone screen where typed text in memory is marked process ended, while a saved copy in storage survives

You switched apps for a minute. Where did your text go?

You are typing a long message. A notification arrives, you open another app to check it, and when you come back, the message box is empty. Nothing you did was wrong. Your phone made a decision about memory, and the app was not ready for it.

This happens to every mobile developer once, usually on a real phone and never on the test one. Let's see why, using a small pretend phone.

Apps are guests, and the phone owns the memory

A computer's memory (RAM) is the fast, temporary space where running programs keep their variables. It is limited, and a phone has less of it than a laptop. Only one app is on screen at a time, so the phone treats everything else as a guest it can ask to leave.

Each app lives in a process: a running copy of the program with its own slice of memory. When the phone ends a process, every variable inside it is gone, including the text that was sitting in your message box.

Both Android and iOS describe an app's life as a handful of states. The names differ, the idea is the same:

IdeaAndroid activityiOS scene
On screen, in useResumedForeground, active
Partly covered or leavingPausedForeground, inactive
Not visible, still in memoryStoppedBackground, then suspended
GoneProcess endedDisconnected / not running

Android's documentation says it plainly: the system can end your app's process while the user is away and your activity is stopped. Apple's documentation says UIKit can disconnect a background or suspended scene at any time to reclaim its resources. Neither promises to warn you first.

Try it: lose a draft on purpose

Type something in the notes app. Press Leave the app, then Open a big game, which uses so much memory that the phone has to free some. Then come back. Run it once with the box unticked and once ticked.

Two things stand out. If you leave and return right away, the text is still there, because the phone did not need the memory. And when the phone did end the process, the only thing that came back was what your code had written to storage before that moment. You cannot save at the moment of death, because nothing is promised to run then.

The same idea in a few lines

You can see the whole mechanism without any phone. In this JavaScript, disk is storage that outlives the app, and setting app = null stands in for the phone ending the process:

const disk = {};

function launch() {
  const app = { draft: disk.draft ?? "" };
  return app;
}

let app = launch();
app.draft = "Buy milk, call Sam";
console.log("typed:   ", JSON.stringify(app.draft));

disk.draft = app.draft;   // save when the screen goes away
app = null;               // the system ends the process

app = launch();           // user taps the icon again
console.log("restored:", JSON.stringify(app.draft));

Running it with Node prints:

typed:    "Buy milk, call Sam"
restored: "Buy milk, call Sam"

Comment out the line disk.draft = app.draft; and the second line becomes restored: "". Same code, same phone, one missing save.

What to do about it in your own app

Save early and when leaving. Android's guide suggests doing heavier saving when the activity stops, and not in onPause, which should stay brief. Apple's guide says to save data when the app leaves the foreground-active state. Neither platform wants you to wait for the end.

Keep the screen's state somewhere that survives. On Android, a ViewModel survives things like rotation, but not the process ending. Jetpack Compose's rememberSaveable is documented to survive both configuration changes and system-initiated process death, and works well for small things like typed text or scroll position. Bigger or longer-lived data, such as a draft you care about, belongs in local storage such as a database or a file.

Test it on purpose. Because the phone only ends processes when memory is tight, you rarely see this at your desk. The simplest manual test is to leave your app, open several heavy apps, and return.

Treat "come back" as "start again". The safest mental model is that every return might be a fresh launch that has to rebuild the screen from saved data. If your app does that well, the lucky case (still in memory) works as well, for free.

The takeaway

A phone app is a guest. It can be asked to leave without notice, and its variables leave with it. Anything a person would be upset to lose has to be written to storage while the app is still alive, which for most apps means as soon as it changes or at the latest when it leaves the screen.

$ ls mobile/

see all →
mobile/list-recycling-why-a-phone-can-scroll-10-000-contacts-while-building-o.md List recycling: why a phone can scroll 10,000 contacts while building only 8 rows A phone can scroll through 10,000 contacts without building 10,000 rows. It builds only enough row views to cover the screen, then recycles… 5 min read · 25 Oct mobile/push-notifications-how-your-phone-buzzes-for-an-app-that-isn-t-running.md Push notifications: how your phone buzzes for an app that isn't running Your phone buzzes with a message from an app that isn't even running. How? Phones keep one shared connection to a push service (Apple's… 4 min read · 22 Oct mobile/offline-sync-what-happens-when-two-phones-edit-the-same-contact.md Offline sync: what happens when two phones edit the same contact You edit a contact's phone number on a plane. Your partner fixes the city on a tablet. Both devices are offline, then both reconnect. Which… 5 min read · 19 Oct mobile/deep-links-why-one-link-opens-an-app-and-another-opens-a-web-page.md Deep links: why one link opens an app and another opens a web page Tap the same link on two phones and one opens an app while the other opens a web page. Who decides, and how? This article explains deep… 4 min read · 16 Oct mobile/pixels-dp-and-points-why-a-button-of-48-is-tiny-on-one-phone-and-fine.md Pixels, dp and points: why a button of 48 is tiny on one phone and fine on another A button you size at 48 looks fine on one phone and tiny on another, because a sharper screen packs more pixels into the same space. This… 4 min read · 10 Oct mobile/how-does-a-push-notification-find-your-phone-when-the-app-is-closed.md How does a push notification find your phone when the app is closed? A push notification does not come from your app. Your phone keeps one connection to a service run by Apple or Google, and your app's server… 5 min read · 6 Oct