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

How to call Android Activity on Click of Slider Menu Item
How to add Alertdialog and link to the button on activity?
Difference between onMeasure, onLayout, OnGlobalLayoutListener
Swipe run by Activity Do OnBackPress
Choose any (#00000000 - #FFFFFFFF) widget background color in configuration activity
Why my Android App doesn't have ActionBar?
Redirecting to a blank page using a click event
How to determinate who is in the end of a line
What is the work around for TYPE_KEYGUARD now that it's been removed?
Pick image from fragment always return resultcode 0 in some devices
HTML Embeded Video Background can't load on Android or IOS
A simple if statement not working
Unable to add multiple markers in a Google Map
Android M Camera Intent + permission bug?
How can I disable a thread with an onClickListener?
Multidex :no class found exception

Categories

HOME
batch-file
asp.net-web-api
httparty
assembly
checkstyle
ns-3
apache-spark-mllib
replace
alsa
big-o
jetty
spring-session
python-multithreading
modal-dialog
octave
enterprise-architect
3nf
symbol
azure-iot-hub
mvc-mini-profiler
video-editing
jersey-1.0
question2answer
imageresizer
mongoid6
qliksense
jboss6.x
siri
stackexchange
ms-access-web-app
ms-access-2003
fractions
angular-translate
tpm
paperjs
comparable
jaws
eviews
cognos-bi
android-sugarorm
modalviewcontroller
sql-delete
python-pptx
latitude-longitude
sql-server-ce
typesafe-config
hackintosh
xmldocument
apache-directory
vaadin-charts
static-cast
semantic-logging
check-mk
bbpress
mouseleave
pybrain
qtplugin
jquery-jscrollpane
observablecollection
udpclient
cda
cycle2
asyncdisplaykit
maybe
linklabel
toran-proxy
pypiserver
vigenere
rubaxa-sortable
evolus-pencil
mod-proxy
linked-tables
aspectj-maven-plugin
bufferedinputstream
mruby
deform
with-statement
live555
vbe
websphere-esb
scrypt
mbunit
ice-cube
cocoalibspotify-2.0
printdialog
unit-of-work
mantle
argouml
qtconcurrent
vfs
garbage
objectquery
stress
role
catransform3d
rmdir
event-receiver
openfaces
strtod
skype4java
alsb
todos
flvplayback
information-management
image-capture

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