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

OpenCV 3.1.0 in android studio (NDK) - updateMotionHistory unresolved
Android Ring Shape with divisions
Firebase loading data for android quiz app
Android soft keyboard hides RecyclerView
How to make android debug mode share the same signature across different machines
Vertical centering imageview in relativelayout
Android Videoview strech not fit in portrait mode
I want to send an message from my Android Watch to my Android Phone. Other way around is working
Backendless Messages.registerDevice return 'success' but device is not listed
basic programming help needed for Firebase, Android
parse the html and show it in webview android
Android navigation drawer selected item default background?
Handling multipart post method request in Nodejs throwing Unexpected Field Error
Why My table is not inflated?
FATAL EXCEPTION: main ( Android + ArrayList<HashMap<String, String>>) [duplicate]
hide RecyclerView when item clicked in the list and get the clicked address

Categories

HOME
asp.net-web-api
design-patterns
nvd3.js
jax-rs
mysql-workbench
vscode-extensions
javacc
cakephp-3.4
parameters
3d-reconstruction
x264
jquery-ui
mvc5
teechart
watson-conversation
activecollab
atlassian
selectize.js
bitnami
aws-sdk
rubymotion
ksh
springfox
resourcebundle
roundup
tiki-wiki
genexus-sd
gp
paragraph
mockjax
weather
activexobject
pdw-file-browser
apiary.io
openerp-7
news
apriori
escpos
drupal-theming
chai-as-promised
convex-optimization
remedy
yii2-user
arules
cvxpy
webjars
microstation
neon
cifilter
uiactionsheet
phpgrid
custom-errors
xenomai
microsoft-reporting
ess
vacuum
alloy-ui
openbabel
procedural-programming
infosphere-spl
android-vibration
ghcjs
tweenlite
with-statement
mintty
dct
amslidemenu
openargs
nevron
workitem
octal
pclzip
dopostback
dajaxice
multiplexing
jsr286
isa-swizzling
jquery-address
android-holo-everywhere
u2netdk
pylucene
uipagecontrol
object-database
opml

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