android


SharedPreferences on first run are not saved when pressing Back Button


I have an Activity that only runs when the application is opened the first time. Every time I open the app again, this Activity won't run(and this is ok). The problem comes when I press the Back Button the first time I open my app. That Activity appears again and I don't want that to happen. How can I prevent this to happen?
This is my SharedPreferences code on my main activity's onCreate(the variable is created outside):
prefs = getSharedPreferences("com.mycompany.myAppName", MODE_PRIVATE);
And this is the onResume method that let my app know if it's running for the first time:
`#Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
// Do first run stuff here then set 'firstrun' as false
Intent myIntent = new Intent(MainActivity.this, StoryActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
MainActivity.this.startActivity(myIntent);
prefs.edit().putBoolean("firstrun", false).commit();
}
}`
Call finish() on your MainActivity before launching the Intent to direct to the next Activity. You can also declare noHistory=true in your AndroidManifest.xml to prevent the Activity from sticking around.
You can also use PackageManager.setComponentEnabledSetting(...) to completely change your "launcher" Activity once it's been viewed.
Because your activity is finish after starActivity.Use this, check the working code:
#Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
prefs.edit().putBoolean("firstrun", false).commit();
// Do first run stuff here then set 'firstrun' as false
Intent myIntent = new Intent(MainActivity.this, StoryActivity.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(myIntent);
}
}

Related Links

Google Map not appear in app after published in Google Play [duplicate]
Android Studio AndroidDebugKey
Android 3Des encryption DESede/CBC/PKCS5Padding similar objective c in ios
RadioGroup not display correct
Android Full Screen Ad (Interstitial) Won't Show When Screen is Locked
Android App: Rate the app link
Hiding TabNavigator when the keyboard appears
Android Studio Simulator Not Working
SQLite, android. Am i doing something wrong on Table definition?
draw Method for Spannable String doesn't get called on Marshmallow
SharedPreferences with encryption decryption (2 SharedPreferences file)
Changing messenger notification sound from my application
change https proxy settings of android studio
Firebase Database crashes Android app
Firebase orderByChild(1) and filter by child two any solutions? [duplicate]
My app stops installing since i added play services library

Categories

HOME
adal
phpmyadmin
pyqt
angular2-material
sh
spring-data-redis
owl-carousel
seo
newrelic
vxml
mapbox
mvc5
performancepoint
commonjs
google-chrome-app
github-enterprise
redis-sentinel
spring-social
openbugs
docx
event-sourcing
alpacajs
aws-sdk
google-picker
sigma.js
oledb
topology
imagemap
visual-studio-extensions
gpio
shared
outliers
linked-data
handler
soot
exchangewebservices
cppreference
jaws
angularjs-material
playstation-portable
login-script
sql-delete
servicestack-text
xajax
latitude-longitude
magento-1.9.2.4
linq-to-objects
magento-1.9.3
dcmtk
xmldocument
oim
automapper-5
gitlist
netbeans6.8
tinymce-3
activeperl
reshape
lsyncd
cmp
rmongo
polyline
taocp
pure-css
cubism.js
angular2-testing
webjars
lrs
mapinfo
observablecollection
riot
php-socket
hadoop-partitioning
phpwebsocket
fink
jnlua
directdraw
toran-proxy
sciruby
computer-architecture
ocra
nsurlsessiondatatask
fits
cakephp-2.7
apache-spark-1.3
rapidsvn
showdialog
teaspoon
create.js
reflector
winginx
scrypt
jeromq
kaleidoscope
intersect
iboutlet
rmiregistry
multiplexing
httponly
jsr286
procedures
ikimagebrowserview
photolibrary
ie-compatibility-mode
globals
dot-emacs
interprocess
data-mapping
xpsdocument
software-lifecycle
movieplayer
wspbuilder

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile