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

Can't access HTTPS from my Android Emulator
Use shell in android to send a picture via telegram or signal
Android expand/collapse view scroll issue
Multicolor android microphone visualizer
Nested JSON and Retrofit flatMap
Firebase return value from datasnapshot
android, block special character in searchview
auto complete textview in firebase list item
Ad finished loading,but banner is not shown (AdMob banner)
Android sound board activity does not start
How to get a log file from end user device in Android?
android - windowSoftInputMode issue
Opening different layouts while implementing SearchView with ListView
Add reverse shadow and elevation to the CollapsingToolbar
Creating an SQLite database for recurring task
Can't populate the keys from firebase into FirebaseListAdapter

Categories

HOME
c++11
apache-flink
case
obfuscation
knitr
database-normalization
maxscript
mvc5
recordrtc
p2p
fhir
distribution
text-mining
watson-conversation
project-intu
automatic-ref-counting
package.json
symfony-2.8
android-7.0-nougat
template-engine
production
symbols
systemtime
tosca
fable-f#
delimiter
outliers
8051
uitapgesturerecognizer
google-cast
autodesk-designautomation
fstream
rselenium
spring-security4
simplecv
html-encode
python-pptx
openproject
git-rewrite-history
cd
sessionstorage
connection-pool
password-protection
verifiable-c
linq-to-objects
c9.io
magento-1.9.3
watchface
apriori
dcmtk
preloader
oracle-service-bus
ntvs
openrasta
corruption
qtranslate
semantic-logging
reshape
type-theory
gpg-signature
structuremap4
android-instrumentation
docpad
scaffolding
gogs
xdoclet
omniauth-facebook
cgi-bin
lasso
baasbox
snackbar
zend-currency
zxspectrum
cross-join
filesplitting
pseudo-class
sql-scripts
angular-carousel
lambda-architecture
renderer
syndicationfeed
time.h
fb.ui
wimax
redis-py
onmousemove
sentestingkit
wcf-configuration
apache-whirr
listactivity
instantiationexception
object-database
jqueryform
duplex
lobo-cobra
mathematical-notation
technical-debt
change-management

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile