Last active
March 23, 2024 18:44
-
-
Save easterapps/7127ce0749cfce2edf083e55b6eecec5 to your computer and use it in GitHub Desktop.
restart android application programmatically
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun triggerRestart(context: Activity) { | |
val intent = Intent(context, MainActivity::class.java) | |
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) | |
context.startActivity(intent) | |
if (context is Activity) { | |
(context as Activity).finish() | |
} | |
Runtime.getRuntime().exit(0) | |
} |
Awesome, this satisfies The app called finish() on an activity very recently.
Also,
Runtime.getRuntime().exit(0)
can be replaced with kotlin.system.exitProcess(0)
Finally! It helped me to delete database and recreate with default data. Thanks.
I can't get it work, sorry
package com.mycompany.myapp.util
import android.app.Activity
import android.content.Intent
import com.mycompany.myapp.ui.MainActivity
fun triggerRestart(context: Activity) {
val intent = Intent(context, MainActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
}
context.startActivity(intent)
context.finish()
kotlin.system.exitProcess(0)
}
@russellhoff how about to replace kotlin.system.exitProcess(0)
with Runtime.getRuntime().exit(0)
?
already tried but with no success
already tried but with no success
Try to explain what is not working. It works on Andoid SDK 31, called from the main thread.
@easterapps thank you very much it works like a charm on all devices ❤️
awesome, help me solve memory leak of DexClassLoader
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sorry i didn't understand how put the code in a project. I must put in a different class? I must put a Receive, Service? Thank you so much!