Use RestartActivity to speed up the restart

This commit is contained in:
Gergely Hegedus 2025-01-20 13:33:06 +02:00
parent 924a0ebb51
commit 54d935c8ee
5 changed files with 63 additions and 33 deletions

View file

@ -1,17 +1,18 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"> <manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<application <application
android:label="experiment_shorebird"
android:name="${applicationName}" android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"> android:icon="@mipmap/ic_launcher"
android:label="experiment_shorebird">
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:exported="true" android:exported="true"
android:hardwareAccelerated="true"
android:launchMode="singleTop" android:launchMode="singleTop"
android:taskAffinity="" android:taskAffinity=""
android:theme="@style/LaunchTheme" android:theme="@style/LaunchTheme"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
android:hardwareAccelerated="true"
android:windowSoftInputMode="adjustResize"> android:windowSoftInputMode="adjustResize">
<!-- Specifies an Android theme to apply to this Activity as soon as <!-- Specifies an Android theme to apply to this Activity as soon as
the Android process has started. This theme is visible to the user the Android process has started. This theme is visible to the user
@ -26,13 +27,19 @@
</intent-filter> </intent-filter>
<meta-data <meta-data
android:name="io.flutter.embedding.android.NormalTheme" android:name="io.flutter.embedding.android.NormalTheme"
android:resource="@style/NormalTheme" android:resource="@style/NormalTheme" />
/>
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN"/> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER"/> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
</activity> </activity>
<activity
android:name=".RestartActivity"
android:exported="false"
android:process=":restart"
android:excludeFromRecents="true"
android:noHistory="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<!-- Don't delete the meta-data below. <!-- Don't delete the meta-data below.
This is used by the Flutter tool to generate GeneratedPluginRegistrant.java --> This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
<meta-data <meta-data
@ -46,8 +53,8 @@
In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. --> In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
<queries> <queries>
<intent> <intent>
<action android:name="android.intent.action.PROCESS_TEXT"/> <action android:name="android.intent.action.PROCESS_TEXT" />
<data android:mimeType="text/plain"/> <data android:mimeType="text/plain" />
</intent> </intent>
</queries> </queries>
</manifest> </manifest>

View file

@ -22,15 +22,13 @@ class MainActivity : FlutterActivity() {
try { try {
if (this.intent.categories.contains(Intent.CATEGORY_HOME)) { if (this.intent.categories.contains(Intent.CATEGORY_HOME)) {
if (isDefaultHomeApp()) { if (isDefaultHomeApp()) {
runViaAlarmManager(getHomeIntent()) RestartActivity.restart(this, getHomeIntent())
} else { } else {
runViaAlarmManager(Intent.makeRestartActivityTask(componentName)) RestartActivity.restart(this, Intent.makeRestartActivityTask(componentName))
} }
} else { } else {
runDirectly(Intent.makeRestartActivityTask(componentName)) RestartActivity.restart(this, Intent.makeRestartActivityTask(componentName))
} }
finishAndRemoveTask()
Runtime.getRuntime().exit(0)
result.success(null) result.success(null)
} catch (e: Throwable) { } catch (e: Throwable) {
result.error("couldn't start activity", e.message ?: "", null) 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 { private fun isDefaultHomeApp(): Boolean {
val intent = Intent(Intent.ACTION_MAIN) val intent = Intent(Intent.ACTION_MAIN)
intent.addCategory(Intent.CATEGORY_HOME) intent.addCategory(Intent.CATEGORY_HOME)

View file

@ -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)
}
}
}

View file

@ -152,7 +152,7 @@ class _MyHomePageState extends State<MyHomePage> {
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
const Text( const Text(
'This is a release 1.9.9#6', 'This is a release 1.9.12#3',
// 'You have times:', // 'You have times:',
), ),
const Text( const Text(

View file

@ -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 # 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 # 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. # 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: environment:
sdk: '>=3.4.3 <4.0.0' sdk: '>=3.4.3 <4.0.0'