DB restore: restart via AlarmManager (startActivity+exit raced, app just closed)

This commit is contained in:
4aiman 2026-09-21 13:44:54 +03:00
commit 492c54990f
2 changed files with 28 additions and 4 deletions

View file

@ -557,10 +557,22 @@ class MainActivity : TauriActivity() {
}
cmd == "restartapp" -> {
// Relaunch the process so the staged DB (pending_restore.db) is swapped in onCreate.
// startActivity()+exit() races (the new activity doesn't come up before the process
// dies — measured: app just closed, no restart). Schedule the relaunch via AlarmManager
// so it fires AFTER this process is gone (the ProcessPhoenix approach).
Log.d("BalDav", "Restarting app (DB restore)")
val intent = packageManager.getLaunchIntentForPackage(packageName)
intent?.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
try {
val intent = packageManager.getLaunchIntentForPackage(packageName)
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK)
val piFlags = android.app.PendingIntent.FLAG_CANCEL_CURRENT or
(if (Build.VERSION.SDK_INT >= 23) android.app.PendingIntent.FLAG_IMMUTABLE else 0)
val pi = android.app.PendingIntent.getActivity(this, 0, intent, piFlags)
val am = getSystemService(ALARM_SERVICE) as android.app.AlarmManager
am.set(android.app.AlarmManager.RTC, System.currentTimeMillis() + 300, pi)
}
} catch (e: Exception) { Log.e("BalDav", "restart schedule failed", e) }
finishAffinity()
Runtime.getRuntime().exit(0)
}
}