Repository navigation
android: Keep a test app's data when Android restores the development host - #4263
Open
madcodelife wants to merge 1 commit into
Open
madcodelife wants to merge 1 commit into
madcodelife wants to merge 1 commit into
Conversation
… host The development host emptied the data directory of a test session's app (`dataDir`) on every `onCreate`, also when Android restored the activity after killing its process, so the app lost its storage on a restore that a packaged app survives. Empty it only on a launch (`savedInstanceState == null`), in an Android-free helper (`TestData`) covered by a JDK test.
huacnlee
approved these changes
Dec 3, 2026
huacnlee
left a comment
Member
There was a problem hiding this comment.
✅ DevActivity now empties a test app's dataDir only on a fresh launch, so an activity that Android restores after killing its process keeps its storage, as a packaged app does.
Reviewed head: 8b587c0
This was a diff review. I did not build anything locally.
No issues found.
TestData.open(root, name, state != null)keeps the old delete on launch.nsh testalways starts the host witham start -S --activity-clear-task, so every testopenstill begins with an empty directory. Only the restore path (savedInstanceState != null) changes.- The deletion is limited to
<cacheDir>/nsh-test/<name>, and the JDK test checks that other apps' directories are left alone. - A restore after "Don't keep activities" or a configuration recreate now also keeps the data. That is the intended packaged-app behaviour, and previously a configuration recreate could silently wipe a test's storage in the middle of a session.
No public API change. One gap: the description does not say whether TestDataTest is wired into any CI script or is run by hand like the other plain-JDK tests.
CI: still running. check (windows-latest) passed, the macOS and Linux check jobs were pending, and changed jobs were skipped at the time of review.
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.
Problem
nsh test --target androidgives each app a data directory of its own (dataDir) and the development host (DevActivity) empties it so the app starts with nothing stored. It emptied it on everyonCreate, including when Android restores the activity after killing its process (savedInstanceState != null, e.g. the process was killed in the background and the user comes back from Recents). The app then lost its storage and caches on restore, unlike a packaged app (AppActivity), whose data survives process death. Process-death recovery (restoring a saved draft, reading persisted state) could not be checked on a device or emulator.Minimal reproduction, with an app that saves
Storage.setItem("draft", "hello")on a press of Save and showsStorage.getItem("draft")when it starts (draft: nonewhen nothing is stored):nsh run <app> --target androidserves the app and installs the development host as<id>(the app's id). Start the host asnsh testdoes, with a data directory:adb shell am start -S --activity-clear-task -n <id>/com.longbridge.nsh.DevActivity --es url ws://127.0.0.1:8790/ws --es dataDir app-0.draft: hello (saved).adb shell input keyevent KEYCODE_HOME, thenadb shell am kill <id>: the process is gone, the task stays in Recents.adb shell am start -n <id>/com.longbridge.nsh.DevActivity(no-S/ clear-task flags) brings the existing task to the front, as Recents does, and Android restores the activity with its saved state and its original extras.Before:
draft: none, the directory was deleted. After:draft: hello.Change
savedInstanceState == null); a restored activity keeps it. The CLI always launches the host witham start -S --activity-clear-task, so every testopenis still a fresh launch with an empty directory.TestData.open) so it can be tested with the plain JDK, likeFileRootsandForeignIntents.docs/dev/tooling.mdand the module comment oftest_android.rssay the directory is kept on restore.No public API changes.
Tests
TestDataTest(runs in the Android workflow's "Compile the Java runtime and run its JDK tests" step): a launch empties the app's directory and leaves other apps' directories; a restore keeps its storage and caches. It fails with the old always-empty behavior.javac --release 17againstandroid-35, and every JDK test passes.mainand from this branch:main: after the restore the app showsdraft: none, andcache/nsh-test/<dataDir>no longer exists.draft: hello, and the directory is the same one (same inode). A newam start -S --activity-clear-tasklaunch afterwards showsdraft: noneagain.nsh test --target android crates/nsh-platform-android/tests/input-press.test.tsxon the emulator: 4 passed.