I have two activities. In the first activity, I have a SharedPreferences with a TextView, which displays the amount of data in the SharedPreferences. These codes are written in the onCreate()
SharedPreferences coin = getSharedPreferences("saved", MODE_PRIVATE);
SharedPreferences.Editors editor2 = coin.edit();
editor2.putInt("t", 0).apply();
editor.commit();
TextView name = (TextView) findViewById(R.id.textview);
Integer a = coin.getInt("t", 0);
name.setText(a.toString());
In the second activity, I wanna add 10 points to the data and update the SharedPreferences. These codes are not inside onCreate().
SharedPreferences coin = getSharedPreferences("saved", MODE_PRIVATE);
SharedPreferences.Editors editor2 = coin.edit();
int a = coin.getInt("t", 0);
int b = a + 10;
editor2.putInt("t", Integer.valueOf(b));
The problem is, after adding 10 units there is no update in the TextView so I don't even know if the 10 units have been added correctly to SharedPreferences or not.
Please help
If I understand, you would like reflect the final value of t in SharedPreference in your first activity after its updated in second activity?
To do that, create a function updateNameTV() which would contain logic to update the TextView with the value of t from SharedPreference
and call that function in onResume() of the FIRST activity like:
#Override
protected void onResume() {
// TODO Auto-generated method stub
updateNameTV();
super.onResume();
}
So when Activity 2 is launched, Activity 1 goes in back of the stack and onPause() gets called.. when we return to Activity 1 this activity is pop back and onStart() followed by onResume() is called.
These are the lifecycle events more here
So the plan is, when onResume() is called we update the TextView value with the latest SharedPreference
And we do that before calling super.onResume() such that update is performed before handing over the control to next lifecycle event. Hope this gives more clarity.
You have to register OnSharedPreferenceChangeListener in your first activity in order to listen to the changes and then update your TextView with the updated value.
Implement OnSharedPreferenceChangeListener in your FirstActivity:
public class FirstActivity extends AppCompatActivity implements
SharedPreferences.OnSharedPreferenceChangeListener {
...
#Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) {
// This method will be called when there is a change in SharedPreference
// Update your textview here
if(s.equals("t")) {
Integer a = sharedPreferences.getInt("t", 0);
name.setText(a.toString());
}
}
}
In your FirstActivity's onStart() and onStop() method, manage the listener:
#Override
protected void onStart() {
super.onStart();
getSharedPreferences("saved", MODE_PRIVATE).registerOnSharedPreferenceChangeListener(this);
}
#Override
protected void onStop() {
getSharedPreferences("saved", MODE_PRIVATE).unregisterOnSharedPreferenceChangeListener(this);
super.onStop();
}
But remember, once you leave your FirstActivity, the listener gets unregistered and you will no longer receive updates. To reflect the updates even after the activity go background, you can just update your textview in your onStart() so that when the FirstActivity starts, the textview gets updated with the latest value from SharedPreferences:
#Override
protected void onStart() {
super.onStart();
SharedPreferences coin = getSharedPreferences("saved", MODE_PRIVATE);
coin.registerOnSharedPreferenceChangeListener(this);
// Also Update the textview here
Integer a = coin.getInt("t", 0);
name.setText(a.toString());
}
Now, any update to the preference from anywhere will also change the textview text as soon as the FirstActivity comes into foreground or launches.
Related
I got a ViewPager which saves the current page position (as the order in it's adapter). When the app goes to the backgroung, the ViewPager seem to lose the position it was last in and when switching to the app again, it starts at fragment with the position 0.
How can I save the current position and set the correct fragment? Hopefully it's something that needs to be done in a single place only and not in every fragment.
OK so it eventually was just something standard. I just overrided OnPause and OnResume. I saved the state with a SharedPreferences in the OnPause method and restored it in the OnResume method.
#Override
public void onPause()
{
super.onPause();
final SharedPreferences.Editor ed = getSharedPreferences("name",
android.content.Context.MODE_PRIVATE).edit();
ed.putInt(INDEX_KEY_STRING, viewPager.getCurrentItem());
ed.commit();
}
#Override
public void onResume()
{
super.onResume();
final SharedPreferences sp = getSharedPreferences("name",
android.content.Context.MODE_PRIVATE);
viewPager.setCurrentItem(sp.getInt(INDEX_KEY_STRING, 0));
}
I have two activities:
ActivityOne
ActivityTwo
ActivityOne opens ActivityTwo
Intent i = new Intent(ActivityOne .this,ActivityTwo.class);
startActivity(i);
ActivityTwo updates some value MyValue in SharedPrefences.
ActivityOne has some TextView field that shows the value of MyValue.
After I close the ActivityTwo I want ActivityOne IMMEDIATELY to update
the TextView according to the new value of MyValue (that was changed in ActivityTwo).
I tried to call to SharedPrefernces and update the TextView view in OnResume() function in ActivityOne,
but somehow it doesn't get called.
Any ideas?
Thank you
More Code:
ActivityTwo updating MyValue:
#Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getSharedPreferences(Settings.sharedPrefencesForSettingsName,0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("MyValue", unspecified.isChecked());
editor.commit();
}
ActivityOne updating TextView:
if (settings.getBoolean("MyValue", true)) {
textViewMine.setText("SomeText");
According to the Android's Coordinating activities documentation, onStop() method of ActivityTwo is executing after onResume() of ActivityOne and that's why you are not getting the updated value from SharedPreference.
Now to solve the problem, move those code snippet from onStop() method to onPause() method as follows...
#Override
protected void onPause() {
super.onPause();
SharedPreferences settings = getSharedPreferences(Settings.sharedPrefencesForSettingsName,0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("MyValue", unspecified.isChecked());
editor.commit();
}
Use startActivityForResult() instead of startActivity().
onActivityResult() will be called as soon as you return to the caller.
See Getting a Result from an Activity in the documentation.
I am creating a calculator application, when my activity is sent to background and then brought to front.The result in it becomes zero. How can i save the data nd retrive it next time hen activity is recreated.
For that specific case, you want to use the Activity lifecycle methods onSaveInstanceState and onRestoreInstanceState. Override both those methods, and then save the necessary data in onSaveInstanceState. In the onCreate or the onRestoreInstanceState check for the existence of the saved data and then restore it to the right place.
Note: you can use preferences or a database as well, but those are generally to be used for data that should last more than a single background/restore cycle. The onSaveInstanceState/onRestoreInstanceState methods are specifically for the case where an Activity gets sent to the background and then restored later.
Use SharedPreferences like :
SharedPreferences preferences = null;
SharedPreferences.Editor editor = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
preferences = PreferenceManager.getDefaultSharedPreferences(this);
}
in onDestory() write :
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
editor = preferences.edit();
editor.putInt(YOUR_KEY, YOUR_VALUE);
editor.commit();
}
and in onStart() write :
#Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
int result = preferences.getInt(YOUR_KEY, YOUR_VALUE);
}
Hope this will help you
I have application with an activity and a service, I need to save some value in the activity and retrieve in the service.
i could save the value using SharedPreferences in the activity, however, when I try to retrieve the value in the BroadcastReceiver, it says getPreferences is undefined for service.
how could I retrieve my value in BroadcastReceiver?
EDITED to reflect change of the original question from Service to BroadcastReceiver.
Instead of using getPreferences(int mode) in the Activity use...
getSharedPreferences(String name, int mode).
The getPreferences(int mode) method is a convenience method for the above and simply passes the Activity class name as the name parameter. This implies it should really only be used for a given Activity to store its own internal preferences and not preferences that need to be global to other app components.
In the case of a BroadcastReceiver the onReceive(...) method is passed a Context parameter so you can use context.getSharePreferences(<some_name>, <mode>) to get the SharedPreferences saved by the Activity.
public class AndroidWalkthroughApp4 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public void onResume() {
// get EditText object
EditText editText = (EditText)this.findViewById(R.id.edit_text);
// get preferences object
SharedPreferences prefs = this.getPreferences(MODE_PRIVATE);
// set text to our saved value
editText.setText(String.valueOf(prefs.getInt("chars", 0)));
// don't forget to do this, or your app will crash!
super.onResume();
}
#Override
public void onPause() {
// get EditText object
EditText editText = (EditText)this.findViewById(R.id.edit_text);
// get preferences object
SharedPreferences prefs = this.getPreferences(MODE_PRIVATE);
// create editor from preferences object
SharedPreferences.Editor editor = prefs.edit();
// save and write length of EditText
editor.putInt("chars", editText.getText().length());
editor.commit();
// don't forget this either!
super.onPause();
}
}
So I have two activities: main with all data and secondary with Preferences. There are two arrays in main activity that I want to have access to from Preferences activity and work with this two arrays. In fact, I need to reset all data in arrays to default. Is there any way to do it?
I've tried to use SharedPreferences because I store all this data in it. I changed values in it from Preferences activity, but data in main activity haven't changed, I guess, because it's not being restarted, just paused.
You can use SharedPreferences and register a OnSharedPreferenceChangeListener and/or update the data in the onResume() method of your Activity.
Here's some basic example:
public class MainActivity extends Activity implements OnSharedPreferenceChangeListener {
private SharedPreferences prefs;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get your SharedPreferences objects
prefs = ...;
}
// This method will be called every time your activty comes to the front
#Override
protected void onResume() {
super.onResume();
// register a listener to get notified when the preferences change
prefs.registerOnSharedPreferenceChangeListener(this);
// do whatever you need to do to update the data
updateData();
}
#Override
protected void onPause() {
super.onPause();
// unregister listener if another activity comes to the front to save battery
prefs.unregisterOnSharedPreferenceChangeListener(this);
}
// this method will be called when some preference changes
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
// do whatever you need to do to update the data
updateData();
}
}