From 54d935c8eee244a09b5bec171d507386331d1b78 Mon Sep 17 00:00:00 2001 From: Gergely Hegedus Date: Mon, 20 Jan 2025 13:33:06 +0200 Subject: [PATCH] Use RestartActivity to speed up the restart --- android/app/src/main/AndroidManifest.xml | 29 +++++++++----- .../experiment_shorebird/MainActivity.kt | 23 ++--------- .../experiment_shorebird/RestartActivity.kt | 40 +++++++++++++++++++ lib/main.dart | 2 +- pubspec.yaml | 2 +- 5 files changed, 63 insertions(+), 33 deletions(-) create mode 100644 android/app/src/main/kotlin/org/fnives/flutter/experiment_shorebird/RestartActivity.kt diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index d448357..156ee27 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,17 +1,18 @@ + + android:icon="@mipmap/ic_launcher" + android:label="experiment_shorebird"> - - + + diff --git a/android/app/src/main/kotlin/org/fnives/flutter/experiment_shorebird/MainActivity.kt b/android/app/src/main/kotlin/org/fnives/flutter/experiment_shorebird/MainActivity.kt index 5f8e10b..eb8d853 100644 --- a/android/app/src/main/kotlin/org/fnives/flutter/experiment_shorebird/MainActivity.kt +++ b/android/app/src/main/kotlin/org/fnives/flutter/experiment_shorebird/MainActivity.kt @@ -22,15 +22,13 @@ class MainActivity : FlutterActivity() { try { if (this.intent.categories.contains(Intent.CATEGORY_HOME)) { if (isDefaultHomeApp()) { - runViaAlarmManager(getHomeIntent()) + RestartActivity.restart(this, getHomeIntent()) } else { - runViaAlarmManager(Intent.makeRestartActivityTask(componentName)) + RestartActivity.restart(this, Intent.makeRestartActivityTask(componentName)) } } else { - runDirectly(Intent.makeRestartActivityTask(componentName)) + RestartActivity.restart(this, Intent.makeRestartActivityTask(componentName)) } - finishAndRemoveTask() - Runtime.getRuntime().exit(0) result.success(null) } catch (e: Throwable) { result.error("couldn't start activity", e.message ?: "", null) @@ -49,21 +47,6 @@ class MainActivity : FlutterActivity() { } } - private fun runDirectly(intent: Intent) { - context.startActivity(intent) - } - - private fun runViaAlarmManager(intent: Intent) { - val pendingIntent = PendingIntent.getActivity( - context, - 4201, - intent, - PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE - ) - val alarmManager = context.getSystemService(ALARM_SERVICE) as AlarmManager - alarmManager[AlarmManager.RTC, System.currentTimeMillis()+100] = pendingIntent - } - private fun isDefaultHomeApp(): Boolean { val intent = Intent(Intent.ACTION_MAIN) intent.addCategory(Intent.CATEGORY_HOME) diff --git a/android/app/src/main/kotlin/org/fnives/flutter/experiment_shorebird/RestartActivity.kt b/android/app/src/main/kotlin/org/fnives/flutter/experiment_shorebird/RestartActivity.kt new file mode 100644 index 0000000..18ce9f5 --- /dev/null +++ b/android/app/src/main/kotlin/org/fnives/flutter/experiment_shorebird/RestartActivity.kt @@ -0,0 +1,40 @@ +package org.fnives.flutter.experiment_shorebird + +import android.app.Activity +import android.content.Context +import android.content.Intent +import android.os.Build +import android.os.Bundle +import android.os.Process + +const val KEY_ORIGINAL_PID = "KEY_ORIGINAL_PID" +const val KEY_INTENT = "KEY_INTENT" + +class RestartActivity : Activity() { + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + + Process.killProcess(intent.getIntExtra(KEY_ORIGINAL_PID, -1)) + + val intent = if (Build.VERSION.SDK_INT > 33) { + intent.getParcelableExtra(KEY_INTENT, Intent::class.java) + } else { + @Suppress("DEPRECATION") + intent.getParcelableExtra(KEY_INTENT) + } + + startActivity(intent) + finish() + } + + companion object { + fun restart(context: Context, intent: Intent) { + val restartIntent = Intent(context, RestartActivity::class.java) + restartIntent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS) + restartIntent.putExtra(KEY_ORIGINAL_PID, Process.myPid()) + restartIntent.putExtra(KEY_INTENT, intent) + context.startActivity(restartIntent) + } + } +} \ No newline at end of file diff --git a/lib/main.dart b/lib/main.dart index 6bc690e..dc51e92 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -152,7 +152,7 @@ class _MyHomePageState extends State { mainAxisAlignment: MainAxisAlignment.center, children: [ const Text( - 'This is a release 1.9.9#6', + 'This is a release 1.9.12#3', // 'You have times:', ), const Text( diff --git a/pubspec.yaml b/pubspec.yaml index 9c17ed3..3829520 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -16,7 +16,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev # https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html # In Windows, build-name is used as the major, minor, and patch parts # of the product and file versions while build-number is used as the build suffix. -version: 1.9.9+199 +version: 1.9.12+202 environment: sdk: '>=3.4.3 <4.0.0'