Storing and Retrieving Values from SharedPreferences on Activity state changes - android

Basically I have a list and I need to remember the offset and load the offset value every time the Activity is restored unless the Activity completely destroyed.
//Inside onCreate
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
Offset = settings.getInt("TheOffset", 0);
//End onCreate
#Override
protected void onPause() {
super.onPause();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", Offset);
}
#Override
protected void onStop() {
super.onStop();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", Offset);
}
#Override
protected void onDestroy() {
super.onDestroy();
//settings.getInt("TheOffset", 0);
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", 0);
}

onPause() will always be called before your activity is placed in the background and/or destroyed, so you do not have to save state in onStop() and onDestroy() as well.
For the state to be preserved in SharedPreferences, you need to add editor.commit() after writing the value. Otherwise it won't be stored. Like this:
super.onPause();
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", Offset);
editor.commit();
You can read more here: http://developer.android.com/reference/android/app/Activity.html#SavingPersistentState

You will only need to save your offset in onResume() and set it to 0 when the activity is going to be destroyed, which you can tell by using isFinishing() in onPause(), like the following:
protected void onPause() {
if(isFinishing()) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("TheOffset", 0);
editor.commit();
}
}
...but I still have no idea what you intend to achieve.

Related

SharedPreferences should remember the content of the TextView when it returns to MainActivity

I have a situation where there is a TextView in MainActivity with some textual content. However, when I go from MainActivity to SecondActivity and return to MainActivity again, the text contained in TextView is lost. I tried to solve this with the help of SharedPreferences and I wrote the code. SharedPreferences does not save when I return from SecondActivity. I really do not see where I am making a problem in this code and I ask your help.
private void SavePreferences(String key, String value){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.apply();
}
private void LoadPreferences()
{
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String textstorage = sharedPreferences.getString("TEXT", "");
mytext.setText(textstorage);
}
You should call LoadPreferences() in onResume() because onCreate() won't be called when you return back from Second Activity
#Override
protected void onResume() {
super.onResume();
LoadPreferences();
}
you need to give name to your preference try below code:SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, PRIVATE_MODE).edit()
editor.putString(key, value);
editor.apply();
editor.commit(); // you can omit this, i use this one
and to get it back SharedPreferences sharedPreferences = getSharedPreferences(PREF_NAME, PRIVATE_MODE);
String textstorage = sharedPreferences.getString(key, "default value"); // default value will be alternate value if you string is not found
mytext.setText(textstorage);

Shared preference with session timeout using countdowntimer

I want to using startcountdown timer method to change preference value but its not worked.
private void startCountdownTimer(final String judul){
countDownTimer = new CountDownTimer(120000, 1000) {
public void onTick(long millisUntilFinished) {
SharedPreferences pref = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("click"+judul, "1");
}
public void onFinish() {
SharedPreferences pref = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("click"+judul, "0");
}
}.start();
}
can i use method ontick and onfinish to change preference like that ?? I want to make session timeout in android actually. so Im using countdown timer to manipulate it.
Use like this is more easy
SharedPreferences pref = getActivity().getSharedPreferences("data", Context.MODE_PRIVATE);
pref.edit().putString("click"+judul, "1").commit();

usea savePreferences in dynamically layout -android

I'm making an app that contains a dynamically layout and it generates textView's in run time, I need that when I close the application all the textView has been added, don't erase; I think I can do that whit savePreferences(), but What parameters I have to use?
private void savePreferences(What parameter here, What parameter here) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor edit = sp.edit();
edit.commit();
}
Thank you
Shared Preferences should work. Try this...
public class sample extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false);
setSilent(silent);
}
#Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
// THIS IS WHERE YOU STORE THE TEXTVIEW DATA
// THERE ARE MANY ' editor.put...(...) ' methods
// ie. editor.putInt(...) , editor.putString(...)
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}

How to retrieve the default value of a preference defined in the XML file

How do you get the default value of a single Android Shared Preference as it is explicitly defined in the corresponding XML file? E.g.:
<CheckBoxPreference
android:defaultValue="false"
android:key="fulldb"
android:summary="No selection rules apply"
android:title="Use Full Database" />
Like this..
public class Calc extends Activity {
public static final String PREFS_NAME = "MyPrefsFile";
#Override
protected void onCreate(Bundle state){
super.onCreate(state);
. . .
// Restore preferences
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
boolean silent = settings.getBoolean("silentMode", false); //default value if nothing is in the preference is the last parameter false.
setSilent(silent);
}
#Override
protected void onStop(){
super.onStop();
// We need an Editor object to make preference changes.
// All objects are from android.context.Context
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("silentMode", mSilentMode);
// Commit the edits!
editor.commit();
}
}
See below:
To Store the Value:
public static SharedPreferences myPrefs;
public static SharedPreferences.Editor prefsEditor;
myPrefs = this.getSharedPreferences("myPrefs",MODE_WORLD_WRITEABLE);
prefsEditor = myPrefs.edit();
prefsEditor.putBoolean("FullResultIsOn", true); // value to store
prefsEditor.commit();
Now to retrive the value:
myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
fullResultSound = myPrefs.getBoolean("FullResultIsOn", false);
Enjoy. :)
Thanks.

Activity that is only launched once after a new install?

I want my app to have an activity that shows instruction on how to use the app. However, this "instruction" screen shall only be showed once after an install, how do you do this?
You can test wether a special flag (let's call it firstRun) is set in your application SharedPreferences. If not, it's the first run, so show your activity/popup/whatever with the instructions and then set the firstRun in the preference.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences settings = getSharedPreferences("prefs", 0);
boolean firstRun = settings.getBoolean("firstRun", true);
if ( firstRun )
{
// here run your first-time instructions, for example :
startActivityForResult(
new Intent(context, InstructionsActivity.class),
INSTRUCTIONS_CODE);
}
}
// when your InstructionsActivity ends, do not forget to set the firstRun boolean
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (requestCode == INSTRUCTIONS_CODE) {
SharedPreferences settings = getSharedPreferences("prefs", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean("firstRun", false);
editor.commit();
}
}
yes, you can fix this problem with SharedPreferences
SharedPreferences pref;
SharedPreferences.Editor editor;
pref = getSharedPreferences("firstrun", MODE_PRIVATE);
editor = pref.edit();
editor.putString("chkRegi","true");
editor.commit();
Then check String chkRegi ture or false

Categories

Resources