Android: Retrieve int saved in PreferenceManager getDefaultSharedPreferences in another activity - android

I am trying to save data using default Shared Preferences, but cannot get this code working.
I cant retrieve an int value in another activity.
In my first activity, in onCreate, I want to create a scorecounter that counts up every time you receive points. It will be initiated on first run.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
if (preferences.getBoolean("first_run", true))
{
editor.putInt("totalpoang", 0);
editor.commit();
preferences.edit().putBoolean("first_time", false);
}
In my Activity B I want to retrieve the value, modify it, and then save it in preferences again.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = preferences.edit();
int poang = preferences.getInt(totalpoang, 0);
editor.putInt("antalpoang", totalpoang);
editor.commit();
However I only get the error totalpoang cannot be resolved as a variable.
I initialized it as a int in the first Activity, and I am trying to retrieve it as an int.
What am I doing wrong?

Change this
int poang = preferences.getInt(totalpoang, 0);
//editor.putInt("totalpoang", 0); // key is "totalpoang"
to
int poang = preferences.getInt("totalpoang", 0);
And then
editor.putInt("antalpoang", poang);

Related

how to pass data to more number of activities

I would like to pass data from one activity to another.If it is two or three activities we can send data via intent.suppose more number of activities are present (approximately 20).how can i pass data from first activity to last activity?
i want to go Activity A-->B-->C-->D-->......Y-->Z
if we send data via intent(put Extra) that is worst method.
is there any other way to send data?
thanks in advance
I would use SharedPreferences for this.
This will be easier because, we can change it anywhere in any activity and access them as needed. And we don't need to pass on each and every activity transition.
Simple example:
To set value in shared preference
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
editor.putString("name", "Nabin");
editor.putInt("idName", 12);
editor.commit();
And retrieve as
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
String restoredText = prefs.getString("text", null);
if (restoredText != null) {
String name = prefs.getString("name", "No name defined");//"No name defined" is the default value.
int idName = prefs.getInt("idName", 0); //0 is the default value.
}
You can refer here more about it.
If you need some data to multiple activities, Just save data into SharedPreference and you will be able to access to all activities.
Here is full tutorial.
Save Data
// Create object of SharedPreferences.
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
SharedPreferences.Editor editor= sharedPref.edit();
//put your value
editor.putString("name", strName);
editor.putString("pwd", strPass);
editor.commit(); //commits your edits
Retrieve Data
SharedPreferences sharedPref= getSharedPreferences("mypref", 0);
String name = sharedPref.getString("name", "");
String password = sharedPref.getString("pwd", "");
If it's not a must case to use activities, you can change activities to fragments, attach them to same activity, cache your data in activity and get it from fragments.

How to share 1 SharedPreferences file between 2 activities

I have got a SharedPreferences file in my Activity1.
void saveDays(){
Log.w(TAG, "Start saveDays");
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putInt("Days", days);
editor.commit();
}
Then I need to use days in my Activity2. So how can I load it?
In Activity2, where you want to get access to that preference:
int days = getPreferences(MODE_PRIVATE).getInt("Days", DEFAULT_DAY);
Where DEFAULT_DAY is the default value to use if there is no "Days" preference available.
You want to name the preferences and access them with that name in both
SharedPreferences prefs =
getSharedPreferences("myPrefs", MODE_PRIVATE);
And then its as simple as retrieving the int...
int days = prefs.getInt("Days", 1);

Android SharedPreferences , how to save a simple int variable [duplicate]

This question already has answers here:
How to use SharedPreferences in Android to store, fetch and edit values [closed]
(30 answers)
Closed 9 years ago.
I am trying for the last hour to save an integer in my Android application. I read that this can be done using the SharedPreferences. However i dont understand why it seems so confusing to do so.
How can i simply save an int variable ? And then when i run the application again , how can i interact with this variable again?
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putInt("your_int_key", yourIntValue);
editor.commit();
the you can get it as:
SharedPreferences sp = getSharedPreferences("your_prefs", Activity.MODE_PRIVATE);
int myIntValue = sp.getInt("your_int_key", -1);
The SharedPreference interface gives you access to the an xml file, and a easy way to modify it through its editor. The file is stored in /data/data/com.your.package/shared_prefs/ and you can access it onlu through this SharedPreference API
public void SaveInt(String key, int value){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(key, value);
editor.commit();
}
public void LoadInt(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
savedValue = sharedPreferences.getInt("key", 0);
}
If you want to save the variable somewhere, you have to write SaveInt("key", 5); With this you will save the value 5, while the first default value is 0. If you want to load it and use it in another activity, you have to write both of these methods there, and call LoadInt(); where you need the variable. The savedValue is a predefined integer (this needs to be declared everywhere you would like to use the saved variable)
This is the example of setting Boolean preferences. You can go with Integer also.
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("firstTime", false)) {
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
Hope this might be helpful.

How to make a persistent counter variable in an Android app?

I'm just starting out.
I'm trying to increment a simple counter, every time I run the project on the emulator.
I thought adding an integer type item in strings.xml would help, but that's final, and can't be modified.
Basically I'd just display in my app's first basic screen:
Started: N where N would be the Nth time I've launched the project from Eclipse.
How can I make such a counter that's persistent across application launches and exits?
Got it:
SharedPreferences pref = getPreferences(MODE_PRIVATE);
int tempN=pref.getInt("N", 0);
tempN++;
SharedPreferences.Editor editor = pref.edit();
editor.putInt("N",tempN);
editor.commit();
msgBox.setText("Started:"+tempN);
One thing I still don't understand, that when I call pref.getInt("N",0), is the key-value pair <N,0> automatically created?
You can use shared preference for that . You can store integer number in shared preference and get value of it when ever you want.
Try this.
SharedPreferences prefs = getSharedPreferences("Share", Context.MODE_PRIVATE );
Editor editor = prefs.edit();
editor.putInt("Value", 1 );
editor.commit();
for get value
prefs.getInt("Value",0);
In your main activity onCreate():
SharedPreferences pref = this.getSharedPreferences();
int count = pref.getInt("your key", 0) //0 is default value.
count++;
SharedPreferences.Editor edit = pref.edit();
edit.putInt("your key", count);
edit.commit();
// display current count

Sharedpreferences - crashes on startup

I'm trying to write an activity that would be able to both write and read sharedpreferences data.
I initiate SharedPreferences at the beginning
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Then this function writes an int to SP and call another function.
public void SetHue(int i)
{
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", i); // value to store
editor.commit();
ApplyHue();
}
this other function should read that int from SP...
public void ApplyHue()
{
int hueInt = preferences.getInt("storedInt", 0);
/// adjust background image hue according to hueInt.
}
I can't simply pass this int from one function to another, because I need other activities to be able to run ApplyHue() function, which should use hueInt from memory.
What do you think might be causing it to crash?
Thanks!
I think you wrote this line in the class before your onCreate method.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
Decalare SharedPreferences preferences; in the class and then in onCreate
preferences = PreferenceManager.getDefaultSharedPreferences(this);
Hopefully your problem will be solved

Categories

Resources