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

JavaScript in a WebView
Show custom dialog when spinner is being clicked
Is it safe for me to save resource id as user preference
Programatically add Done button to Android MediaController subclass
WebView horizontal scrolling fails
Mystery URL decoding of certain characters - Android URI / Google App Engine
How can i create splash screen not using thread or something like that in Android?
registering a component with a id (`contentview`) which has already been used.ensure the existing cmpnt has been destroyed `Ext.Component#destroy()
How force update data in ListView from Adapter?
Multiple custom views in Scroll view Android
ZXing Camera Setting Focus and Light Settings via IntentIntegrator
Unknown host exception returned when attempting to connect to the server on the HTC Rezound and only on 4g
Android: How to get context of calling activity in an IntentService?
How to save a Drawable in the resource folder?
SMS body is not changing its text in intent
Why we need declare parcelable object in AIDL file?

Categories

HOME
uml
mjml
hessian
tivoli
permissions
nstableheaderview
32bit-64bit
opengl-es-3.0
rtf
php-5.6
automatic-ref-counting
speech-synthesis
eip
http-post
alpacajs
symfony-2.8
dreamweaver
nodemcu
boost-icl
asciidoctor-pdf
ios10.2
php-mysqlidb
comparator
definitelytyped
has-and-belongs-to-many
openmdao
dotspatial
xbox
dumpbin
rselenium
carriage-return
jwplayer6
sql-delete
watchman
release
projects-and-solutions
demo
cin
google-data-api
typesafe-config
urlsession
spring-mvc-test
vmware-fusion
attributeerror
datacachefactory
branching-and-merging
tcc
oim
clarifai
vugen
x-ray
podscms
ngmaterial
offline.js
cudd
watch-os-2
gogs
xdoclet
pre-build-event
qiime
cfchart
kango-framework
stream-processing
php-socket
project-organization
rasterize
jnlua
spring-retry
terracotta
haskell-warp
graphics2d
matlab-deployment
user-permissions
cakephp-2.7
actionfilterattribute
exceldatareader
android-cookiemanager
crowd
sonarqube-5.0
mobility
kissfft
android-framework
multiautocompletetextview
jeromq
ebay-lms
oocss
route-provider
chart-director
http-status-code-410
qtconcurrent
faye
subgraph
fusefabric
apache-whirr
jdb
convention
filemerge
android-holo-everywhere
automationelement
lr
cgbitmapcontextcreate
nhprof
resharper-6.0
dot-emacs
application-verifier
sifr
lalr
fault-tolerance
flvplayback
image-capture

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App