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

Monitor every battery state change even with screen off
How to set an Android TableRow column's width to fill available space?
Beginning android developer problems
unfortunately app has stopped working- android emulator
Problems with facebook login android
Need an if statement to stop MediaPlayer object only if it's playing
How to remove space before first item of gallery?
How to get GNU C listed in preferences
Design advice Android image buttons
White screen partially covers my content in webview android
RadioButton vs Spinner
How to refresh the listview content in timely manner
google maps android api error authorization failure app displaying blank map
Image from container is going down on action
Different font width in #string of TextView
How to retrieve Bitmap with Picasso or Universal Android Loader?

Categories

HOME
service-worker
cobalt
wakanda
github-for-windows
datastore
mjml
camera
hessian
saml-2.0
limit
maxscript
retrofit2
syntaxnet
gson
mod-security
line
cmdb
speech-synthesis
radgridview
entitymanager
google-data-studio
production
rubymotion
angular-routing
android-maps
android-things
tizen-tv
carrierwave
panoramas
autolisp
card.io
twos-complement
classcastexception
installshield-le
xajax
sql-server-ce
ideascript
release
projects-and-solutions
apache-stanbol
knime
xmldocument
dynamic-linq
linter
dotnet-httpclient
reportservice2010
vugen
zenhub
arules
flyout
cpu-cache
taocp
visual-studio-2008-sp1
reporting-services-2012
lua-telegram-bot
nss
createprocess
pushbots
nio2
piracy-prevention
dynamics-nav-2016
rewrite
microsoft-reporting
poppler
uncompress
sql-server-data-tools
typeconverter
zxspectrum
gamekit
document-repository
apache-spark-1.3
beatsmusic
sql-scripts
entry-point
paw
personalization
live555
malformedurlexception
android-framework
gae-quotas
mysql-error-1044
workitem
flex++
android-icons
pagedown
video-codecs
nokia-s40
jdb
multiplexing
fitbounds
virtualpathprovider
role
multiplatform
unattended-processing
qglwidget
resharper-6.0
spread
fdf
flvplayback

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