Skip to content

Start each re-check from a clean app

Each ticket’s re-check starts from your app’s recorded launch. Without a reset, what one replay saved can change the next ticket’s starting screen. Add a reset handler to your debug or test build so each replay starts with clean app state.

Call this function at the very start of launch, before reading any stored state. It reacts to the -b8eReset launch argument only in debug builds. It removes the app’s UserDefaults domain and the contents of Application Support, Caches and Documents.

import Foundation
func resetForRecheck() {
#if DEBUG
guard ProcessInfo.processInfo.arguments.contains("-b8eReset") else { return }
guard let bundleId = Bundle.main.bundleIdentifier else {
fatalError("Cannot reset an app without a bundle identifier")
}
UserDefaults.standard.removePersistentDomain(forName: bundleId)
let manager = FileManager.default
let directories: [FileManager.SearchPathDirectory] = [
.applicationSupportDirectory, .cachesDirectory, .documentDirectory
]
do {
for directory in directories {
for root in manager.urls(for: directory, in: .userDomainMask) {
guard manager.fileExists(atPath: root.path) else { continue }
for item in try manager.contentsOfDirectory(
at: root, includingPropertiesForKeys: nil
) {
try manager.removeItem(at: item)
}
}
}
} catch {
fatalError("Re-check reset failed: \(error)")
}
#endif
}
resetForRecheck()

In your launcher activity’s onCreate, clear saved state before setContentView. This example handles the b8eReset intent extra only in debug builds. Keep state initialization after the reset. If your Application class reads stored state, defer those reads until after the activity reset in test builds.

import android.os.Bundle
import java.io.File
override fun onCreate(savedInstanceState: Bundle?) {
val reset = BuildConfig.DEBUG && intent.getBooleanExtra("b8eReset", false)
if (reset) {
val preferences = File(applicationInfo.dataDir, "shared_prefs")
preferences.listFiles()?.filter { it.extension == "xml" }?.forEach {
check(getSharedPreferences(it.nameWithoutExtension, MODE_PRIVATE)
.edit().clear().commit()) { "Could not clear preferences" }
}
databaseList().forEach { name ->
check(deleteDatabase(name)) { "Could not delete database $name" }
}
listOf(filesDir, noBackupFilesDir, cacheDir).forEach { directory ->
directory.listFiles()?.forEach { file ->
check(file.deleteRecursively()) { "Could not delete ${file.name}" }
}
}
}
super.onCreate(if (reset) null else savedInstanceState)
setContentView(R.layout.activity_main)
}

With no handler in the app, re-checks still run. A ticket whose screen no longer matches its recording stays ready for re-check with the reason, never a false done.