-2

How do I programmatically "clear the app data " and then “restart” an Android app? Already tried with below links:

Restart android app after cache clear

Android restart app after clearing cache and data

How do I programmatically "restart" an Android app?

stack Learner
  • 1,318
  • 3
  • 18
  • 35
  • It will clear the app data but wont restart the app. – stack Learner Mar 27 '19 at 10:48
  • If you need this for debugging then this plugin should do it https://plugins.jetbrains.com/plugin/7380-adb-idea otherwise the approach would be to run adb commands using Java https://www.swtestacademy.com/adb-commands-java-device-manipulation/ – cutiko Mar 27 '19 at 11:49

2 Answers2

0

After calling .clearApplicationUserData() application is killed.

So you can have only one of those - clear app data, or restart app.

If you want just to clear DB or some files stored by app that you know of, you can do it manually, and then restart app, but if you want to completely erase all user data you need to shut app, and maybe before that inform user that he will need to manually start app again.

Lukas Novicky
  • 921
  • 1
  • 19
  • 44
0

I don't know about "clearing app data" but you can restart the app by using this method.

 private void restartApp() {
    Intent intent = new Intent(getApplicationContext(), YourActivityHere.class);
    int mPendingIntentId = 2;
    PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
    AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
    System.exit(0);
}
bensadiku
  • 436
  • 3
  • 15