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 Custom Keyboard for pin code Activity
Kotlin data classes and nullable types
Adobe Flash Professional CS5.5(AS3) with Distriqt Compass always says “The native extension context could not created”
Firebase: Pull random data from Firebase to RecyclerView (android)
Tablelayout and tablerow not setting up properly
No cached version listing for com.parse.tools:gradle:1.+ available for offline mode
Set the minimum timeout for input voice with recognizer speech
New Android Studio don't let me build project with API 23
Communicate with Android bluetooth device via NodeJS and Termux
Not getting Request From Ion (Uploading Image From Android to Spring Server)
Marshmellow GPS coordinates onChangeLocation not calling
How to get track the streaming error JwPlayer Android
android - static shortcut: Possibility of parsing data and start Main Activity from specific code?
getting “rror.Error:Execution failed for task ':app:transformResourcesWithMergeJavaResForDebug'. >”
background image overlay too large
Problems understanding RX philosophy (RxJava, RxJS, Rx…) on android

Categories

HOME
embedded
httparty
linkedin
string
forms
mstest
jwplayer
newrelic
cakephp-3.4
joomla3.0
computer-science
rcloud
mql5
fhir
php-5.6
gkturnbasedmatch
renjin
package.json
mathprog
string-concatenation
angular-routing
ckfinder
itunes
google-cloud-print
apktool
corenlp-server
aws-codecommit
volatile
cognos-bi
git-squash
autolisp
linkedhashmap
jett
flatmap
news
azure-arm
xmldocument
opennms
wcftestclient
manova
bluez
scalding
context-sensitive-grammar
colorbar
android-testing
reporting-services-2012
lrs
neon
jquery-jscrollpane
dup
delta
avspeechsynthesizer
appscale
google-query-language
phpwebsocket
findfirst
invoke-sqlcmd
phpgrid
jnlua
libvlc
.bash-profile
rpart
evolus-pencil
mod-proxy
baasbox
pyobjc
typeconverter
actionfilterattribute
packet-sniffers
cbind
dsym
websphere-esb
oocss
modular
asdf
poker
diem
video-codecs
joomla3.1
tree-balancing
uimanageddocument
listitem
jdb
cross-database
luabind
winmain
drawtobitmap
objective-j
openfaces
web-application-project
cuda.net
google-wave
change-management
great-circle

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