How to restart android app programmatically
Asked Answered
P

1

9

I want to re-start my app through Pending intent. Below code is not working.

val intent = Intent(this, Activity::class.java).apply {
     flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
}
val pendingIntentId = 1
val pendingIntent = PendingIntent.getActivity(this, pendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT)
val mgr = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val timeToStart = System.currentTimeMillis() + 1000L
mgr.set(AlarmManager.RTC, timeToStart, pendingIntent)
exitProcess(0)

Target version is 31, so updated pending intent with PendingIntent.FLAG_MUTABLE still not working. I searched in many links related to this but no luck.

Restarting Android app programmatically

Force application to restart on first activity

https://www.folkstalk.com/tech/restart-application-programmatically-android-with-code-examples/#:~:text=How%20do%20I%20programmatically%20restart,finishes%20and%20automatically%20relaunches%20us.%20%7D

In Nov 2022, When target version is 31 & min sdk version is 29, above pending intent code is not restarting the App.

Any clue why above pending intent is not working or any other suggestion apart from re-launching the activity ?? I don't want to re-launch using startActivity(intent)

Phila answered 25/11, 2022 at 11:5 Comment(4)
Try Runtime.getRuntime().exit(0) or System.exit(0)Jabez
It might helps #6609914Jabez
Check it out as well its Android 12 compatible github.com/JakeWharton/ProcessPhoenixJabez
It is compulsory to use pending intent.Hemocyte
C
7

When you want to restart the entire app you could use the very easy libary: ProcessPhoenix

You can just simply insert the Library and execute:

ProcessPhoenix.triggerRebirth(context);

or with a specific intent:

Intent nextIntent = //...
ProcessPhoenix.triggerRebirth(context, nextIntent);

This is the easiest way to restart an android app programatically.

Chara answered 29/11, 2022 at 11:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.