Skip to content
tezvyn:
Menu

How does Android manage ContentProvider lifecycle and threading across processes?

Source: developer.android.comHardHow cards are made

Tests Android IPC and provider architecture. Strong answers explain lazy per-process instantiation via AMS, ContentResolver as the client binder proxy, and query executing on a binder thread. Red flag: claiming the provider runs in the caller process.

What's really being asked

Deep understanding of Android inter-process communication, the ContentProvider framework, and process boundary mechanics. Interviewers want to know if you understand that a ContentProvider is not just a data abstraction but a system-managed component with a distinct lifecycle tied to its hosting process, and whether you can distinguish between the client-side proxy and the actual provider instance.

The full answer

First, lifecycle and instantiation. The Android system lazily creates the provider in its declared process the first time any client process requests it. If the process is not running, zygote forks it, the Application instance is created, and ContentProvider.onCreate is called before the first query is handled. The provider remains a singleton for the lifetime of that process. Second, the role of ContentResolver. ContentResolver is the client-side API that validates the URI, communicates with ActivityManagerService to locate the correct provider process, obtains an IBinder reference, and marshals arguments across the process boundary. It does not instantiate the provider directly. Third, threading on the provider side. When a query arrives from another process, the system dispatches it to a thread in the Binder thread pool within the provider's process, not the main thread. Fourth, threading on the caller side. ContentResolver.query is a blocking IPC call, so the caller must invoke it from a background thread to avoid jank or ANR.

The mistakes people make

Claiming the provider object lives in or is created inside the querying app's process. Saying query runs on the main thread in the provider process. Believing ContentResolver directly constructs or caches the provider instance rather than acting as a proxy. Asserting that every query triggers a new provider instance or that the provider process shuts down immediately after the query.

What usually comes next

How would you implement a long-running query in a provider without blocking the binder thread? What happens if the provider process dies while the cursor is being used? How does same-process access differ, and does it skip IPC? How would you enforce permissions across process boundaries?

A concrete example

App B calls contentResolver.query with a URI owned by App A. The system checks that App A's process is running; if not, it starts the process. App A's ActivityThread installs the provider, calls its onCreate, and publishes it to the system. The binder proxy in App B blocks while App A's system dispatches the call to a binder thread executing MyProvider.query. The results are written into a CursorWindow and sent back over Binder. App B receives the cursor and can iterate over it. Throughout this, App B's ContentResolver never holds the actual provider object, only a proxy.

Interview question

Which statement accurately describes what happens when an app first queries a ContentProvider residing in a different process?

  • a.The provider object is lazily instantiated inside the querying app's process, with onCreate called before the first query.
  • b.The provider process is started if needed, but query executes on the main thread of the provider process to maintain thread safety.
  • c.ContentResolver directly constructs and caches the remote provider instance to avoid repeated IPC overhead.
  • d.The provider's process is started if needed, onCreate is invoked, and the query executes on a binder thread in that process.Correct
Why?

The correct answer matches the card's description that the system lazily starts the provider process, calls its onCreate, and dispatches cross-process queries to a binder thread. The distractor placing query on the provider's main thread is a common misconception explicitly flagged as a red flag.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles