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

Is there any setting in android for seeing ANR for an app if app UI becomes unresponsive for say 10sec
No resource found that matches the given name:attr 'colorAccent'
Create transparent toolbar over map fragment
Gridview setChoiceMode(GridView.CHOICE_MODE_SINGLE) does not work
ViewPager with Picasso not loading inside RecyclerView Row
Fatal Exception: java.lang.IllegalArgumentException width and height must be > 0
SVG/VectorDrawable issue in android
How to show all photos folder when user select gallery option
Cobertura throws ClassNotFoundException in Android-project
define manifest place holders in build.gradle
I use ToolBar in my app and I want to custom the width of menu but I do not how to do?
Android: One item on the list view is not showing text properly
using geofencing in android or ios
PDFTron is throwing crunch file error
How to solve Unchecked call to 'GsonRequest(Params)' as a member of raw type?
Access runtastic app data

Categories

HOME
batch-file
search
localization
spark-streaming
case
numbers
battery
oracle-apex-5
xbee
retrofit2
trac
fosrestbundle
project
endeca
mod-security
opengl-es-3.0
xcodebuild
spring-social
tput
code-generation
eip
coin3d
modbus-tcp
fileserver
android-7.0-nougat
gmm
topology
replication
lilypond
dataflow-diagram
monit
jasmine-node
rpgle
user-defined-functions
volatile
record
login-script
gojs
python-import
reverse
xajax
flatmap
recurrent-neural-network
cin
setup.py
datacachefactory
wmp
preloader
dbnull
smoothstate.js
file-sharing
vaadin-charts
term
sonarlint-eclipse
identification
google-cse
stdmap
er-diagram
payload
janus
vulcanize
cortex-m
aspen
httpruntime.cache
contentcontrol
java-money
avisynth
sha512
airbrake
botan
cwrsync
omnifaces
nsurlsessiondatatask
j-security-check
zxspectrum
phpldapadmin
ejb-2.x
dbaccess
angular-gettext
sip-server
human-computer-interface
nsmenu
verold
reflector
matrix-inverse
dct
mutual-authentication
syndicationfeed
sphinxql
c++builder-xe5
tlf
onload-event
bjyauthorize
android-sdk-2.3
dopostback
uimanageddocument
wcf-configuration
user-tracking
htdocs
filemerge
twitterizer
cac
non-clustered-index
complex-event-processing
wysiwym
sqlobject
information-management
xslcompiledtransform

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile