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

Make View Clickable after Animation in Android
How to detect android soft keyboard “Will Show” event
Unit Testing Fragment has caused code inflation
Android | Dagger 2. Injecting different subclasses into Fragment depending of a condition
Alarm Manager in Android is every 10 minutes
Ionic 2 - NavController canGoBack() allways undefined (Android)
getMaxAmplitude from Visualizer
Android Tab Activity giving Firebase Errors
Determining Image Filesize Before Scaling
Inflating YouTubePlayerSupportFragment from resource layout and adding to main activity
Keyboard is hiding Views
Firebase Admin - Uncaught exception in Firebase Database runloop (4.1.5)
Error inflating class AppCompatButton due to a missing attribute in TypedArray
Is it possible to delete a marker 10 seconds after it's placed on map automatically?
Android Storage Access Framework: Which path should I use to save a file?
Update my app on the Play Store

Categories

HOME
microsoftgraph
apache-flink
apache-jena
fpga
parameters
web-hosting
visual-studio-emulator
jquery-ui
vert.x
trac
dkim
java.util.logging
google-form
supervisor
code-generation
qsqlquery
altera
powershell-v5.0
html5-appcache
jquery-ias
named-entity-recognition
systemtime
angular-routing
qliksense
jquery-callback
itunes
anki
turfjs
wacom
vue-chartjs
getlasterror
engine.io
docfx
salesforce-communities
xcode7.3
imagenet
jpad
kill
linqpad
apache-spark-dataset
dapper-simplecrud
flask-restful
clarifai
reportservice2010
fclose
launchctl
custom-font
azure-cli
boxing
check-mk
payload
rackspace-cloud
universal-analytics
jtree
django-1.10
slackware
node-request
pytables
mongodb-php
beamer
xdoclet
appscale
data-protection
vhosts
r-package
rewrite
ess
dock
cgi-bin
rpart
avahi
ms-access-2000
bufferedinputstream
osc
grouping-sets
variadic-templates
addressing-mode
administration
big-ip
post-commit-hook
entry-point
psr-4
kissfft
file-not-found
clickbank
solace-mq
mvc-editor-templates
node-blade
windows-98
asplinkbutton
android-loadermanager
role
pylucene
cuteeditor
dataform
duplex
information-management
cardspace
great-circle

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