-1

I am doing an android application, and I'm trying to handle a restart method where the application needs to reset and delete its data saved in local. I've already tried the System.exit(0) method, but the application keeps running in backgroung. Instead, the thing I want to do is to completely close the application(also the application task) and then re-open it. Is there a way to do this or not?

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • 1
    No, there isn't. But you might post a notification that will open your app when the users opens it. – Iulian Popescu Jun 05 '20 at 10:18
  • That's a good idea man, I will try and let u know if it works. – Alessandro Vendrame Jun 05 '20 at 10:19
  • This question is already answered here: https://stackoverflow.com/questions/6609414/how-do-i-programmatically-restart-an-android-app – Priyank-py Jun 05 '20 at 10:42
  • 3
    Does this answer your question? [How do I programmatically "restart" an Android app?](https://stackoverflow.com/questions/6609414/how-do-i-programmatically-restart-an-android-app) – Priyank-py Jun 05 '20 at 10:43
  • Also, Android 10 added some restrictions to only start activities only while your app is in foreground. This might also impact how your app is working after a possible "restart" https://developer.android.com/guide/components/activities/background-starts – Iulian Popescu Jun 05 '20 at 11:00

2 Answers2

0

I dont think you need to kill the app instead clear application data !! Try this ..

private void clearAppData() {
    try {
        // clearing app data
        if (Build.VERSION_CODES.KITKAT <= Build.VERSION.SDK_INT) {
            ((ActivityManager)getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData(); // note:it has a return value!
        } else {
            String packageName = getApplicationContext().getPackageName();
            Runtime runtime = Runtime.getRuntime();
            runtime.exec("pm clear "+packageName);
        }

    } catch (Exception e) {
        e.printStackTrace();
    } 
}
0

You could try ProcessPhoenix (https://github.com/JakeWharton/ProcessPhoenix/blob/master/README.md) although this is not intended for production code.

codebod
  • 686
  • 6
  • 17
  • Link only answers are pretty bad since they are not useful anymore if the link breaks. Please consider adding more context to it, or moving it to a comment. – Iulian Popescu Jun 05 '20 at 11:02
  • I am suggesting use of an open source library on github.com. If the link breaks then the library is likely not available anyway. – codebod Jun 05 '20 at 11:05