Repository navigation
Fix NullPointerException in ActivityScenario.close() for finishing activities in a transient lifecycle stage. - #2518
Open
copybara-service[bot] wants to merge 1 commit into
Open
copybara-service[bot] wants to merge 1 commit into
copybara-service[bot] wants to merge 1 commit into
Conversation
copybara-service
Bot
force-pushed
the
piper_1003085587
branch
from
October 21, 2026 16:28
806643c to
55f6200
Compare
…tivities in a transient lifecycle stage. ActivityScenario.moveToState() (and therefore close()) assumed that once the main thread is idle the activity is in a steady lifecycle state. That is not always true: lifecycle transitions are driven by the system, and the main thread can be idle while a transition is still pending. For example, an activity that finishes itself while stopped can receive onRestart() but not onStart(), and then stay in the RESTARTED stage until the system destroys it. A common way to hit this is launchActivityForResult() + getResult() followed by close(), since getResult() returns as soon as the result is delivered, before the activity is destroyed. In that window moveToState() failed with "NullPointerException: Current state was null unexpectedly. Last stage = RESTARTED". When the activity is in a transient stage but is finishing (so it is guaranteed to be destroyed), moveToState() now waits for it to settle instead of failing. The wait is skipped on the main thread (e.g. Robolectric), where the transition cannot progress, and for activities that are not finishing, which keeps the previous behavior for those cases. PiperOrigin-RevId: 1003085587
copybara-service
Bot
force-pushed
the
piper_1003085587
branch
from
October 21, 2026 16:47
55f6200 to
7ba05ac
Compare
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix NullPointerException in ActivityScenario.close() for finishing activities in a transient lifecycle stage.
ActivityScenario.moveToState() (and therefore close()) assumed that once the
main thread is idle the activity is in a steady lifecycle state. That is not
always true: lifecycle transitions are driven by the system, and the main
thread can be idle while a transition is still pending. For example, an
activity that finishes itself while stopped can receive onRestart() but not
onStart(), and then stay in the RESTARTED stage until the system destroys it.
A common way to hit this is launchActivityForResult() + getResult() followed by
close(), since getResult() returns as soon as the result is delivered, before
the activity is destroyed.
In that window moveToState() failed with
"NullPointerException: Current state was null unexpectedly. Last stage = RESTARTED".
When the activity is in a transient stage but is finishing (so it is
guaranteed to be destroyed), moveToState() now waits for it to settle instead
of failing. The wait is skipped on the main thread (e.g. Robolectric), where the
transition cannot progress, and for activities that are not finishing, which
keeps the previous behavior for those cases.