0
try {

    // clearing app data
    String packageName = getApplicationContext().getPackageName();
    Runtime runtime = Runtime.getRuntime();
    runtime.exec("pm clear "+packageName);


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

    Intent intent = new Intent(BaseActivity.this, SplashActivity.class);
    startActivity(intent);
}

I want to open new activity after clear data code on button click, this above is my button click code but when I run it, it clears the data but does not open a new activity. Please help thanks in advance

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

You can try the below code. But it has one limitation as clearApplicationUserData() requires minimum SDK version 19.

Intent intent = new Intent(BaseActivity.this, SplashActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                | Intent.FLAG_ACTIVITY_CLEAR_TASK
                | Intent.FLAG_ACTIVITY_NEW_TASK);

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);

AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent);

System.exit(0);
((ActivityManager)getSystemService(Context.ACTIVITY_SERVICE)).clearApplicationUserData();
Muthuraja
  • 551
  • 5
  • 13
  • your code is not working, actually in my application I use gplus login so I want gplus logout with clear data code, which is not working with your code, gplus account is still login with this code, please provide me code regarding to my concept – Priya Patil Nov 21 '18 at 15:13
  • then replace last line of my code with yours (i.e) which you used in try catch block to clear the app data – Muthuraja Nov 22 '18 at 04:46