android


Trouble using sharedPreferences between two activities


I am trying to save a date in one activity and then have that date put in a textView in another activity. I am not sure about how to get the two activities to communicate with each other.
In file called report.java I have this method that gets the date and save it in sharedPrefernces.
private void updateLabel() {
date.setText(fmtDate.format(dateAndTime.getTime()));
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("date", date.getText().toString()); // value to store
editor.commit();
}
I am trying to figure out how to get my file called inspection use this to populate a textView
The problem I think I am having is with getting the correct name for the report file.
public static final String PREF_FILE_NAME = "report";
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
then I have this code on a method called onResume()
#Override
public void onResume() {
super.onResume();
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String strDate=preferences.getString("date", date.getText().toString());
date.setText(strDate);
}
You are saving the value to two seperate preference files.
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);
Use only one.
Why not use the default preference file that is accessible by all classes/activities of your app?
SharedPreferences preference = PreferenceManager.getDefaultSharedPreferences(yourContext);
preferences.edit().putString(YOURKEY, yourStrValue);
This way you are not creating extra preference files in your app that you have to remember which values are stored in which files. Definately makes life easier.

Related Links

Saving an image and displaying it
How to set project file name in IntelliJ IDEA?
Activity onDestroy onCreate methods
Each button opens new activity
Programmatically retrieve per tag network usage data on Android
Visual Studio Crashes (File Not Found) When Creating New Android Project
Can we implement Packet Extensions in iOS using Robbiehanson's XMPPFramework?
Ionic push notifications how to trigger onNotification event if app is in the background?
Cannot open activity as Dialog in Andorid
Unity Android run in background
Farsi Layout in run-time and change gravity
Implement Youtube player fragment inside other fragment
Watch emulator size of phone, how to resize?
Getting false positive while checking whether alarm manager is set
GoogleApiClient - Connected / Selected User Account
Eyes and mouth are not detected in photos captured by camera in android

Categories

HOME
jpa
awk
ng-idle
dns
ecmascript-6
rdbms
pda
battery
edge
xbee
entity-framework-core
big-o
gembox-spreadsheet
nstableheaderview
lanczos
pkcs#11
drop-down-menu
siddhi
mxgraph
redis-sentinel
pybossa
linear-algebra
esoteric-languages
solrcloud
direct3d11
dynamically-generated
stackexchange
fragment-backstack
intersystems-cache
exchangewebservices
paperjs
errbot
twiml
dotspatial
android-sugarorm
linkedhashmap
reverse
mnist
openerp-7
google-knowledge-graph
xcode7.3
news
apache-stanbol
tasm
android-exoplayer
vaadin-charts
oim
pom.xml
robomongo
globalize
identification
rackspace-cloud
jtree
chromium-os
angular2-testing
autosar
pacemaker
microstation
async.js
scaffolding
lz77
beanstalk
maybe
inf
uncompress
skspritenode
mirror
diff3
gamekit
openbabel
rapidsvn
machine-instruction
wcm
insight.database
jjaql
winginx
terminate
getopt
kogrid
css-reset
android-icons
insert-id
stacky
swfobject
device-width
web-notifications
pdfviewer
vfs
cakephp-1.2
paginator
oracle-enterprise-linux
flex-mobile
clrstoredprocedure
cloudfiles
uideviceorientation
ie-compatibility-mode
facebook-iframe
eclipse-marketplace
django-piston
glui
lalr
purepdf
mathematical-notation
sun
xpsdocument
lemmatization

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