android


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,
((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();
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();
After calling clearApplication data, application is killed.Thats why MainActivity doesn't called.
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.

Related Links

I've got a weird bug when using a ViewPager inside a Fragment
“RadioGroup” must be followed by either attribute specifications, “>” or “/>”
is there any side effect of creating multiple database files for a single android application?
How to access shared file
Record video with a set duration with getUserMedia using Phone Camera
Everytime I do a “git status” it tells me that I have modified about 800 files that I haven't touched at all
PerspectiveTransform and Crop Rectangle in Android using openCV
Android TableLayout duplication on orientation change
Android : Child layouts occupying the whole width of screen
How to fix 414(Request-URI Too Long) error in android Asynctask?
How to insert a list of values in a column in excel sheet using aspose library in android?
Can't finish my app onbackpressed
How to set child item in expandable list view in android using nearby location
File download does not work for me
Removing <br> using XmlPullParser Android
Geofencing with only GPS does not work in android

Categories

HOME
eclipse
facebook-oauth
jpa
checkstyle
linkedin
nvd3.js
redux-form
rdbms
character-encoding
composite-primary-key
pclxl
ms-access-2016
newrelic
yandex
entity-framework-core
zip
jax-ws
drop-down-menu
text-mining
tput
frequency-distribution
linear-algebra
fileserver
sse
charles
breadth-first-search
springfox
squashfs
backpack-for-laravel
question2answer
react-toolbox
inno-download-plugin
roslyn
epsilon
direct3d11
custom-post-type
ms-access-web-app
fragment-backstack
errbot
cppreference
dotspatial
git-squash
papaparse
watchman
amazon-ses
health-monitoring
diacritics
p4merge
odoo-website
skylink
escpos
double-click
xbmc
lampp
yii2-api
tinymce-3
singleinstance
qtranslate
static-cast
java-6
oracleclient
git-flow
libjpeg
lsyncd
choice
omxplayer
lrs
jclouds
swizzling
google-query-language
http-status-code-401
kango-framework
thread-sleep
realsense
toast
decimalformat
grgit
wiredep
lasso
eoferror
mirror
dnssec
fail2ban
openbabel
apache-spark-1.3
android-cookiemanager
easyrtc
webfinger
paw
angular-carousel
cancellation
functional-java
gnumeric
authlogic
artemis
font-awesome-4.0.0
objectquery
inkcanvas
isa-swizzling
urlrewriter
workflow-services
cuteeditor
nhprof
ruby-1.8
jqueryform
build-environment
wysiwym
perl5.12
fault-tolerance
fdf
entitykey

Resources

Encrypt Message