Skip to content
tezvyn:
Menu

ContentProvider Lifecycle During Cross-Process Queries

Source: developer.android.comHardHow cards are made

Tests knowledge of Android's IPC, process management, and threading for ContentProviders. The ContentResolver asks AMS to find/start the provider's process. The provider's onCreate() runs on the main thread, but the query() itself runs on a binder thread.

What's really being asked

This question probes your understanding of Android's core inter-process communication (IPC) mechanisms, specifically how the system manages component lifecycles across process boundaries. It's not just about knowing what a ContentProvider is, but how it's brought to life by the system when needed. The interviewer is looking for precision regarding process creation, component instantiation, thread management, and the roles of ContentResolver and ActivityManagerService (AMS).

The full answer

A strong answer walks through the sequence of events in order. First, the client app calls ContentResolver.query(). This is a local method call within the client's process. Second, the ContentResolver implementation uses a binder proxy to communicate with the ActivityManagerService (AMS). It's the AMS's job to resolve the URI to a specific ContentProvider component. Third, the AMS checks if the provider's host process is already running. If not, it starts the process. Once the process is ready, the system instantiates the ContentProvider class within that new process and calls its onCreate() method. Crucially, onCreate() is always called on the application's main thread. Fourth, after the provider is instantiated, the system dispatches the query() call to it. This call is executed on a binder thread from the system's binder thread pool within the provider's process, NOT on the main thread. This is a key detail for thread safety and avoiding Application Not Responding (ANR) errors.

The mistakes people make

A major red flag is stating that the ContentProvider.query() method runs on the main thread of the provider's process. This is incorrect and would lead to terrible performance and ANRs for any non-trivial query. Another common mistake is failing to mention the role of the ActivityManagerService as the central coordinator that finds and starts the provider's process. Simply saying "the ContentResolver talks to the ContentProvider" is too simplistic for a senior role; it misses the entire IPC and process management aspect. Finally, some candidates assume the provider's process is always running, which isn't true; it's instantiated on demand.

What usually comes next

"What happens if multiple apps query the same ContentProvider simultaneously? Is a new instance created for each?" (No, it's a singleton within its process). "How would you handle a long-running query inside your ContentProvider to avoid blocking the binder thread for too long?" (Perform the work on a separate worker thread and use a mechanism like a CancellationSignal to handle cancellation). "What is the purpose of the attachInfo method in a ContentProvider?" (It's called by the system before onCreate() to pass context and provider information).

A concrete example

Imagine a Contacts app (App A) exposing a ContentProvider and a Messaging app (App B) querying it for a contact's phone number. When App B calls context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ...), its ContentResolver sends an IPC request to the Android system. The ActivityManagerService sees that the authority belongs to App A. If App A's process is not running, AMS starts it. The system then instantiates App A's ContactsProvider, calls its onCreate() on App A's main thread, and then executes the query() method on a binder thread within App A's process. The resulting Cursor is returned back across the process boundary to App B.

Interview question

When a client queries a ContentProvider in another process, on which threads do the provider's `onCreate()` and `query()` methods execute within that provider's process?

  • a.`onCreate()` executes on the main thread, while `query()` executes on a binder thread.Correct
  • b.Both `onCreate()` and `query()` execute on a binder thread.
  • c.`onCreate()` executes on a binder thread, while `query()` executes on the main thread.
  • d.Both `onCreate()` and `query()` execute on the main thread.
Why?

`onCreate()` is a standard component initialization callback that always runs on the main thread. However, the `query()` request arrives via an IPC binder call and is executed on a binder thread from a system-managed pool, not the main thread, to prevent ANRs.

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