android / android/android-test

finish() from onStop() doesn't set ActivityScenario's state to DESTROYED

Open
#978 6 comments 3 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
1.2k
Forks
342
Avg merge
11h 29m
Merged PRs (30d)
2

Description

### Description

```
public class FinishOnStopActivity extends Activity {
@Override
protected void onStop() {
super.onStop();
finish();
}
}
```

```
...
@Test
public void finishOnStopActivityShouldBeDestroyed() throws Exception {
try (ActivityScenario scenario = ActivityScenario.launch(FinishOnStopActivity.class)) {
// Call onStop()
scenario.moveToState(State.CREATED);
ShadowLooper.runUiThreadTasks();

// This line succeeds, but the DESTROYED check fails.
// scenario.onActivity(activity -> assertThat(activity.isFinishing()).isTrue());
assertThat(scenario.getState()).isEqualTo(State.DESTROYED);
}
}
...
```

With this test, the State is actually CREATED even though the activity is marked as finishing.

### Expected Results

The test should pass, since the activity is being destroyed.

### Actual Results

The test doesn't pass, the state remains `CREATED`.

### AndroidX Test and Android OS Versions

1.4.0-beta02
I tested on Robolectric

Contributor guide

Open the contributing guide

Research direction

Start with the ActivityScenario state transition exercised by the FinishOnStopActivity reproduction, and run the provided test on Robolectric. Trace what happens after moveToState(State.CREATED) and the queued UI-thread tasks. Done means an activity that calls finish() from onStop() is reported as State.DESTROYED rather than CREATED.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile, testing-qa
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.