Skip to content Skip to sidebar Skip to footer

Android Restart App After Clearing Cache And Data

I have an app which requires data cleaning to work better again. I am clearing data using this, ((ActivityManager)MainActivity.this.getSystemService(ACTIVITY_SERVICE)).clearApplic

Solution 1:

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

Solution 2:

Create new ApplicationClass like below

publicclassApplicationClassextendsApplication {

privatestaticApplicationClass instance;

@OverridepublicvoidonCreate() {
    super.onCreate();
    instance = this;
}

publicstaticApplicationClassgetInstance() {
    return instance;
}
}

Add Application class to Application tag of manifest

android:name=".ApplicationClass"

<applicationandroid: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"><activityandroid:name=".MainActivity"><intent-filter><actionandroid:name="android.intent.action.MAIN" /><categoryandroid:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>

And use this code for clearing data

Intentintent=newIntent(MainActivity.this, MainActivity.class);
         intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
                 | Intent.FLAG_ACTIVITY_CLEAR_TASK
                 | Intent.FLAG_ACTIVITY_NEW_TASK);
         PendingIntentpendingIntent= PendingIntent.getActivity(ApplicationClass.getInstance().getBaseContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
         AlarmManagermgr= (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();

Solution 3:

For Clear cache try reference link

Try below code for restart app

Intenti= 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.

Post a Comment for "Android Restart App After Clearing Cache And Data"