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 view preferenceFragment as card (cardView)
All the pictures from the gallery in a view — Android
PANIC: Missing emulator engine program for 'x86_64' CPUS
How to change the position of a view dynamically created
why is my ic_launcher icon smaller than GooglePlay App's icons?
how to encrypt database in android? [closed]
Showing Android Wear style AlertDialog
How to create an API specific environment for Android SDK/Eclipse?
Check out Project from Version Control Fails
Architecture of Android app to display data from WS
Free Barcode Scanning Alternatives for Xamarin Android Development
How I can make a list view to send touch events to the items in Android?
Windows Azure Service Client
Handler postDelay() new Runnables create garbage?
Exclude few Urls from deeplinking
Place button next to framelayout

Categories

HOME
windows
notepad++
microsoftgraph
jmx
jelastic
alarm
cublas
rds
maxscript
x264
tvos
code-coverage
recordrtc
fosrestbundle
g++
octave
dkim
appcelerator-studio
text-mining
project-intu
nancy
dynatrace
plink
magnetic-cards
selectize.js
bitnami
capistrano3
easyphp
sigma.js
bourbon
backpack-for-laravel
systemtime
r-mice
event-store
mat
progress-bar
handler
tizen-tv
alm
twos-complement
instantiation
xajax
alphabetical
google-knowledge-graph
ideascript
linq-to-objects
cin
stringtokenizer
knime
jide
openresty
term
reportservice2010
google-cse
corruption
scalding
yii2-user
spring-data-couchbase
git-flow
bbpress
google-photos
winusb
adafruit
macaulay2
avspeechsynthesizer
respondcms
state-restoration
decimalformat
phonertc
batik
node-serialport
diff3
user-permissions
cryptographichashfunction
at-job
android-cookiemanager
filesplitting
browserify-shim
machine-instruction
device-tree
jama
cancellation
android-usb
visual-studio-express
eclipse-kepler
hibernate-entitymanager
artemis
node-blade
jquery-1.9
diem
joomla-template
robotlegs
intersect
joomla3.1
mkv
pstack
monotouch.dialog
os.system
apache-whirr
usertype
stress
urlrewriter
filemerge
undefined-index
ie-compatibility-mode
instantiationexception
maven-1
addchild
zend-rest
echo3
mathematical-notation
contentpresenter
idatareader

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