From f707c6984b97bdf93b8b3f96e3502b1e854f5f17 Mon Sep 17 00:00:00 2001 From: Super12138 <70494801+Super12138@users.noreply.github.com> Date: Wed, 21 Feb 2024 13:19:48 +0800 Subject: [PATCH] Fixed the bug of being unable to restart app --- .../todo/views/settings/SettingsActivity.kt | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/app/src/main/kotlin/cn/super12138/todo/views/settings/SettingsActivity.kt b/app/src/main/kotlin/cn/super12138/todo/views/settings/SettingsActivity.kt index 0435292..3b4b5e2 100644 --- a/app/src/main/kotlin/cn/super12138/todo/views/settings/SettingsActivity.kt +++ b/app/src/main/kotlin/cn/super12138/todo/views/settings/SettingsActivity.kt @@ -1,5 +1,6 @@ package cn.super12138.todo.views.settings +import android.content.Context import android.content.Intent import android.os.Bundle import android.os.Process @@ -77,11 +78,7 @@ class SettingsActivity : BaseActivity() { view?.let { Snackbar.make(it, R.string.need_restart_app, Snackbar.LENGTH_LONG) .setAction(R.string.restart_app_now) { - val intent = Intent(context, MainActivity::class.java).apply { - flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK - } - context.startActivity(intent) - exitProcess(0) + restartApp(context) } .show() } @@ -143,8 +140,7 @@ class SettingsActivity : BaseActivity() { .setTitle(R.string.restore_successful) .setMessage(R.string.restore_need_restart_app) .setPositiveButton(R.string.ok) { _, _ -> - Process.killProcess(Process.myPid()) - exitProcess(10) + restartApp(context) } .setNegativeButton(R.string.cancel, null) .setCancelable(false) @@ -167,5 +163,13 @@ class SettingsActivity : BaseActivity() { } } } + + private fun restartApp(restartContext: Context) { + val intent = Intent(restartContext, MainActivity::class.java).apply { + flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK + } + restartContext.startActivity(intent) + exitProcess(0) + } } } \ No newline at end of file