
Android Screen Recovery: Restore Useful Context After the Process Ends
Plan Android recovery around small UI state, durable business data, current permissions, and tests that distinguish rotation from process loss.
App Development
Reconnect Android activity result callbacks after recreation while preserving pending task identity and preventing duplicate business actions.

LET’S BUILD SOMETHING USEFUL
Talk to ATARAXY DEVELOPERS (SMC-PRIVATE) LIMITED about your website, app, or custom software.
Opening another activity creates a pause in your application’s workflow. A user chooses a document, captures a photo, or completes a step in another screen, then expects to return to the same task. During that interval, Android may recreate the calling activity or its process. A callback that existed only because a button was recently pressed may no longer be available when the result arrives.
The Activity Result APIs help separate registration from launching. Your application still owns the meaning of the result and the business action it triggers. Consider attaching a selected document to a service request. Receiving a document reference is one event; confirming the request, retaining permitted access, uploading the bytes, and preventing a second attachment are separate responsibilities. Designing those boundaries makes recreation a recoverable state instead of a source of missing or duplicated work.
Android’s official guidance says the callback must be registered unconditionally whenever the activity is created. When multiple launchers are registered, keep their registration order consistent across recreation. The launch itself can remain conditional on a user action. Registration must happen before the activity or fragment is created, while launching requires its lifecycle to have reached CREATED. Follow the Activity Result documentation for the specific host component and contract you use.
Avoid putting registration inside the click handler or a branch that depends on data that might load differently after recreation. Give each launcher a stable purpose: one for selecting an attachment, another for capturing a photo, for example. Do not use the position of a changing list item to decide which callback receives a result. Route the delivered result to application state that identifies the pending task, rather than assuming the visible screen still represents the same request.
Before launching, record a small operation description: the request identity, account identity, operation token, and the type of result expected. Use an appropriate saved-state or durable storage mechanism for the lifetime the operation needs. An in-memory ViewModel alone should not be treated as durable storage across process death. Avoid saving large file contents in UI state; save the identifiers and minimal recovery information needed to resolve the operation safely.
For example, a user selects “Attach document” on request R-204. When the picker returns, the app should know that the result belongs to R-204 even if the list has refreshed or another request is currently visible. If the pending context cannot be restored, show a clear recovery choice instead of attaching the document to whichever row is selected now. If the account has changed, require the user to restart the action in the correct account context. A valid platform result does not establish current application authorization.
Each contract has its own output and cancellation behavior. Handle the documented empty or unsuccessful result without reporting a completed action. Returning from a picker without choosing a document should leave the request unchanged and let the user try again. An unavailable provider, unreadable URI, or rejected file is a different outcome and may need an explanation. Keep the messages specific enough that the user understands whether anything was saved.
Validate the returned content through the access available to the app. Do not assume that a filename proves the content type or that a returned reference will remain readable forever. If the selected contract and provider support persistable access, handle that mechanism deliberately; otherwise copy or process the content within the access lifetime your workflow supports. Keep a visible pending state while that work occurs. A callback finishing successfully should not prematurely display “Attachment uploaded” when the network transfer has not started.
Represent the operation with understandable states such as awaiting selection, validating, uploading, attached, cancelled, and failed. Associate transitions with the operation token. This makes it possible to reject a repeated attempt to apply an already completed result without relying on whether the screen happens to be visible. It also gives the UI a stable explanation after rotation or navigation. Keep the selected document’s identity separate from a temporary progress indicator.
If applying the result calls a server, coordinate duplicate protection there too. A repeated client submission should refer to the same intended attachment operation, using an API contract designed for that purpose. Do not claim that callback registration alone makes an external side effect happen exactly once. A request can succeed while its response is lost. On recovery, read the server’s operation status before creating another attachment. If the server has no operation lookup or duplicate-safe contract, surface the uncertainty instead of silently retrying a potentially completed write.
The result handler should update the application model; the screen should render that model when it is active. Avoid directly mutating a view reference that belonged to the previous activity instance. When the user returns to the service request, show the attachment’s actual state and a relevant action. A failed upload might offer Retry or Remove selection, while a cancelled picker generally needs no persistent warning banner.
Also consider deliberate navigation. A user may leave the request while an external activity is open, or the request may be closed by another authorized person. Recheck whether adding an attachment is still allowed. If it is no longer allowed, retain only the temporary data required to explain and clean up the attempt. Do not quietly redirect the result to a new request. AI-assisted classification of the selected document can be an optional later step, with its own consent and data handling, rather than a prerequisite that obscures the basic attachment outcome.
Test the whole round trip, not just the callback in a unit test. Launch the external activity, recreate the caller, return a result, and confirm that the original task receives one application-level operation. Include cancellation, multiple launcher types, account changes, missing saved context, provider read failures, and a server response lost after success. Use controlled test tools for process recreation; distinguish that from force-stopping the app, which is a different scenario.
Inspect both the UI and backend records. One visible attachment can conceal duplicate server objects, while a successful backend write can be hidden behind a stale loading state. Record operation tokens in privacy-conscious diagnostics without logging file contents or sensitive provider references unnecessarily. Keep a test proving that callback registration remains stable when optional screen features are disabled. This catches refactors that move registration into a conditional branch and only fail after a lifecycle event that everyday manual testing rarely exercises.
Reliable activity results require two connected designs: stable callback registration and recoverable business state. Register consistently, persist the pending task’s identity, handle cancellation and invalid content separately, and make applying a result an explicit operation. Verify the server outcome before repeating uncertain writes. The user should return from an external screen to an understandable task state, even when the original activity no longer exists. That continuity comes from preserving intent and validating the result against current context, rather than assuming the callback alone completes the workflow.
LET’S BUILD SOMETHING USEFUL
Talk to ATARAXY DEVELOPERS (SMC-PRIVATE) LIMITED about your website, app, or custom software.