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 program crash with signal 11 and fault address
Memory leak in asynctask called repeatedly
How to fix the issue if app is crashed in android?
Make background span across multiple GridLayout children
What return on fail googleCloudMessaging.register() method
adb is not recognized as an internal or exteral command error in Android studio
Can sinch call emergency numbers like 911?
Android store and retrieve items from xml
Can we include User ID and Password with the intent to launch second application from first application?
Extract widgets from android source code
Deploy Module to App Engine option is not available on Android Studio
Compile ffmpeg with AMR decoding
Show different view depending on Internet connexion
java.lang.RuntimeException: Binary XML file line: You must supply a layout_width attribute
Building android app on Qt using additional library (Assimp)
Is there anybody can help me ? I want to choose some sound ( anywhere in the phone) and send information of this sound to another Activity to play it [closed]

Categories

HOME
scikit-learn
jmeter
webdriver
youtube-data-api-v3
mstest
solaris
spring-data-redis
alarm
pdfbox
google-cloud-functions
rds
hugo
x264
google-cloud-datalab
discrete-mathematics
endeca
appcelerator-studio
pega
dynatrace
qsqlquery
symbol
http-post
modbus-tcp
alpacajs
null-pointer
vuejs
git-extensions
identity-management
apache2.4
squashfs
cgbitmapcontext
systemtime
event-store
resourcebundle
tiki-wiki
slidetoggle
comparable
google-earth
google-cast
turfjs
r-grid
imx6
jpad
unordered-map
dbnull
signal-strength
openal
openrasta
netbeans6.8
convex-optimization
podscms
custom-font
context-sensitive-grammar
sundials
mach-o
mathcad
vcloud-director-rest-api
omxplayer
builtins
reporting-services-2012
spring-cloud-feign
pre-build-event
http-status-code-401
upx
thread-sleep
np-spring
exim4
qt5.2
phonertc
binomial-theorem
slash
mod-proxy
typemock
dulwich
linked-tables
google-refine
bufferedinputstream
fits
zxspectrum
cocos3d
lambda-architecture
jjaql
renderer
koala
process-explorer
abstract-data-type
fb.ui
xcode3.2
cakephp-1.2
jsr286
isa-swizzling
unattended-processing
qglwidget
load-time
eclipse-marketplace
pos-for-.net
lobo-cobra
escrow
software-lifecycle

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