androidalarmmanagerandroid-pendingintent

How to restart android app programmatically


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)


Solution

  • 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.