Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 3    Views: 34

QC
Aspiring developer
Profile
Posts: 50
Reg: Apr 20, 2013
Milwaukee, WI
6,650
11/19/14 02:25 PM (9 years ago)

Persistent Data - sharing data between src code in an app

In one source code I save and read in persistent data in value pairs just fine. So I am saving a person's name in the oncreate: SharedPreferences preference1 = getPreferences(0),MODE_WORLD_READABLE; SharedPreferences.Editor editor1 = preference1.edit(); mSaved_Person_First_Name.setText("", TextView.BufferType.EDITABLE); editor1.clear(); editor1.commit(); onResume: SharedPreferences prefs1 = getPreferences(0), MODE_WORLD_READABLE; String restoredText = prefs1.getString("text", null); if (restoredText != null) { mSaved_Person_First_Name.setText(restoredText, TextView.BufferType.EDITABLE); int selectionStart = prefs1.getInt("selection-start", -1); int selectionEnd = prefs1.getInt("selection-end", -1); if (selectionStart != -1 && selectionEnd != -1) { mSaved_Person_First_Name.setSelection(selectionStart, selectionEnd); } } The edit, save work just fine in the first program. I am trying to read it in the second program in the app to use and not sure how to do this. I put this in the OnCreate SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences prefs1 = getPreferences(0), MODE_WORLD_READABLE; String restoredText = prefs1.getString("text", null); But the restoredText is null. Can anyone guide me as to how I can read it the persistent data? Has anyone done this. Thanks in advance.
 
Dusko
Veteran developer
Profile
Posts: 998
Reg: Oct 13, 2012
Beograd
22,680
like
11/19/14 02:41 PM (9 years ago)
>the second program in the app In Buzztouch parlance, that should be "second screen", I guess. If that is so, then it pays to pay attention to the rest of the framework. In BT_strings.java you will find what you need, a pair of functions: BT_strings.setPrefString and BT_strings.getPrefString. For instance, in the "first" screen / plugin, I put BT_strings.setPrefString("fullPath", "http://www.bestappbuilders.com/uniimages/flight.mp3"); BT_strings.setPrefString("audioName", "Flight"); In the second screen / plugin, I get those values through: fullPath = BT_strings.getPrefString("fullPath"); audioName = BT_strings.getPrefString("audioName"); To see further uses, search for, say, setPrefString in Eclipse (Search option of the main menu), and you will be able to learn more.
 
QC
Aspiring developer
Profile
Posts: 50
Reg: Apr 20, 2013
Milwaukee, WI
6,650
like
11/19/14 05:01 PM (9 years ago)
I will take a look Thanks!
 
QC
Aspiring developer
Profile
Posts: 50
Reg: Apr 20, 2013
Milwaukee, WI
6,650
like
11/25/14 04:24 AM (9 years ago)
Well that got me started this with the site below helped me get how this works straight in my mind and get my data shared. http://stackoverflow.com/questions/6186123/android-how-do-i-get-sharedpreferences-from-another-activity Thanks, K
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.