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

Android listView with CustomAdapter inside another listview
Updating MasterDetailPage's Header on Android using Prism for Xamarin Forms
Multiple onFocusChange receive
Android Studio HAXM Installer Error
change popup menu header style
Android Remote Debugging
App crashing while selecting check box in extensible list view in android
Why drawable-hdpi-v4 is automatically generate in build\intermediates\res\merged and how to disable it
Android Adding a polyline to map error
Can not draw circle with stroke in android
android.view.InflateException: Binary XML file 9
Change background color Bottom Bar android
video of wordpress website not working on Android but working well on iphone and web
Width & height of a WatchViewStub
How to set app level cache directory for OkHttp Client
Using paramiko on a android python App

Categories

HOME
eclipse
oracle12c
awk
mjml
solver
appcelerator
alarm
permissions
emscripten
limit
mapbox
google-cloud-datalab
frontend
crystal-lang
g++
openstreetmap
reactive-programming
mautic
cmdb
selenium-ide
gource
magnetic-cards
capistrano3
internationalization
mvc-mini-profiler
html5-appcache
jquery-ias
systemtime
parcelable
roslyn
joptionpane
direct3d11
dynamically-generated
gmock
froala
google-cloud-print
beautifier
amazon-quicksight
corenlp-server
autolisp
android-sugarorm
github3.py
instantiation
html-encode
protein-database
cin
android-softkeyboard
apache-spark-dataset
phpdbg
httrack
ntvs
builder
cnc
mongotemplate
corruption
singleinstance
subnet
template-toolkit
greatest-n-per-group
jodd
expectations
jtwig
nservicebus5
query-by-example
hadoop-partitioning
turbo-prolog
ellucian-scribe
halcon
jnlua
tfs-workitem
forwarding
ocra
umlgraph
gamekit
cross-join
sip-server
entry-point
cuba
malformedurlexception
xml-dsig
jjaql
draw2d-js
artemis
modular
route-provider
scmmanager
alice
panda3d
matlab-load
clrstoredprocedure
msr
maven-1
web-garden
pos-for-.net
data-mapping
lobo-cobra
scrubyt
appliance

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