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

FFMPEG android compile error
Android Device Chooser: Cannot Detect Device
Cannot dismiss dialog after download completes
Is there any way of clearing the launcher activity from App's Activity history programmatically by setting some intent flag?
How to show a dialog box three days once in an android application
how to make background colored button touchable or appear to clickable in android
Anyone knows how change package name of Telegram app?
Adreno-GSL and AdMob Crashes
Including row at the bottom of Navigation drawer
I can't read .txt files from the raw folder
File last access time on Android
Custom ActionBar and RoboGuice injection
Surface view z-index changing after OnResume/OnPause Android
How to insert current date in sqlitedatabase
Skip error urls android
Android PullToRefreshGridView doesn't scroll to the first item

Categories

HOME
uml
eclipse
scikit-learn
fpga
ns-3
case
youtube-data-api-v3
jelastic
can
replace
telegram-bot
mapbox
vsm
jquery-ui
nuget-package
special-characters
spin
g++
openstreetmap
fhir
android-securityexception
windows-ce
openbugs
flatpak
do-while
package.json
mvc-mini-profiler
git-extensions
digital-signature
jmp
sar
spring-async
jquery-ias
pagespeed
pimcore
object-storage
direct3d11
jboss6.x
fragment-backstack
mat
handler
paperjs
bioinformatics
left-join
autolisp
sammy.js
gnat-gps
getpixel
mnist
batching
xcode7.3
imagenet
protein-database
gdata
verifiable-c
google-data-api
numberpicker
ssrs-2014
jboss-4.2.x
openrasta
robomongo
reportservice2010
facebook-pixel
dbcontext
rmongo
cvxpy
winusb
envi
flex4.6
dotnetnuke-7
greatest-n-per-group
dimension
query-by-example
jaunt-api
terracotta
binomial-theorem
alloy-ui
rrule
cakephp-2.7
angular-gettext
sonarqube-5.0
sql-scripts
punycode
genymotion-call
imagefilter
jama
xml-namespaces
lnk
spring-remoting
dopostback
mkv
extconf.rb
fitbounds
convention
nhprof
addchild
django-piston
skype4java
information-management
contentpresenter
leader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App