3

I have an app which requires data cleaning to work better again. I am clearing data using this,

((ActivityManager)MainActivity.this.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();

But cannot restart the app. I tried adding intent after clearing data. But since the app closes after clearing data. I think that code is unreachable.

((ActivityManager)MainActivity.this.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
Toast.makeText(MainActivity.this,"Reloading...",Toast.LENGTH_SHORT).show();
startActivity(new Intent(MainActivity.this,MainActivity.class));
finish();
Nikhil
  • 1,212
  • 13
  • 30
Vivek
  • 165
  • 2
  • 12

3 Answers3

1

After calling clearApplication data, application is killed.Thats why MainActivity doesn't called.

nivesh shastri
  • 430
  • 2
  • 13
1

Create new ApplicationClass like below

public class ApplicationClass extends Application {

private static ApplicationClass instance;

@Override
public void onCreate() {
    super.onCreate();
    instance = this;
}

public static ApplicationClass getInstance() {
    return instance;
}
}

Add Application class to Application tag of manifest

android:name=".ApplicationClass"

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:name=".ApplicationClass"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

And use this code for clearing data

Intent intent = new Intent(MainActivity.this, MainActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                 | Intent.FLAG_ACTIVITY_CLEAR_TASK
                 | Intent.FLAG_ACTIVITY_NEW_TASK);
         PendingIntent pendingIntent = PendingIntent.getActivity(ApplicationClass.getInstance().getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
         AlarmManager mgr = (AlarmManager) ApplicationClass.getInstance().getBaseContext().getSystemService(Context.ALARM_SERVICE);
         mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent);
         System.exit(2);
         ((ActivityManager)MainActivity.this.getSystemService(ACTIVITY_SERVICE)).clearApplicationUserData();
jagapathi
  • 1,635
  • 1
  • 19
  • 32
  • Tried this, the call to `System.exit(2)` I think prevents `clearApplicationUserData()` from executing. – Robert May 15 '18 at 21:14
  • .ClearApplicationUserData(); force closes the app. So i restarted the app before this line. But. Code works perfectly. – jagapathi May 15 '18 at 23:41
  • 3
    This answer is incorrect. This code doesn't work and will not work. clearApplicationUserData clears all alarms. – Dmytro Karataiev May 25 '18 at 09:22
  • @DmytroKarataiev Can you tell me where the documentation is for your statement: "clearApplicationUserData clears all alarms"? Because the [official documentation](https://developer.android.com/reference/android/app/ActivityManager.html#clearApplicationUserData%28%29) doesn't say this. – Brian Jan 22 '20 at 16:50
  • @Brian I don't remember the source, but I confirmed this at the time by manually testing it. Anyway, this is a terrible solution and shouldn't be used in production. – Dmytro Karataiev Jan 22 '20 at 17:06
  • @DmytroKarataiev Than you for the update. I, however, need to use it to fulfill a software requirement to remove all of our app data from the device. – Brian Jan 22 '20 at 18:01
  • @Brian if you really want to clear the app - you can't restart it, everything gets deleted. If you just need to clear preferences/database - do it one by one. – Dmytro Karataiev Jan 24 '20 at 01:08
  • @DmytroKarataiev For "security reasons" I need to wipe every single piece of data generated by our app. – Brian Jan 24 '20 at 17:05
  • @Brian you are of out luck, there is no way to relaunch the app. Unless you create another app -> switch to it during the clearing of your main app, delay for a second and launch your app again from there. This is crazy, just explain to people that this is impossible and link this discussion lol – Dmytro Karataiev Jan 24 '20 at 19:57
0

For Clear cache try reference link

Try below code for restart app

Intent i = getBaseContext().getPackageManager()
         .getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();

Note: If you use clear the app data app doesn't restart. You have to restart it again manually.

Community
  • 1
  • 1
Bhavin Patel
  • 872
  • 13
  • 30