Android issue with boolean on shared preferences
SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
boolean isSlide=spref.getBoolean("SLIDE_SHOW",false);
//in isSlide am getting the right value but the if statement is not working
if(isSlide==true){
//if true the page will slide but it works in false too
}
i tried in this link Issue with boolean on shared preferences but the answer is not clear
settings code:
<PreferenceCategory android:title="Slide Show" >
<CheckBoxPreference android:title="Slide Show" android:key="SLIDE_SHOW" />
</PreferenceCategory>
You may want to check if your SharedPreference variables are active. This is an example on how to set the SharedPreference.
To write into SharedPreference:
SharedPreferences prefs = getSharedPreferences("myPref", 0);
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("myBool", true);
editor.commit();
To read from SharedPreference:
SharedPreferences prefs = getSharedPreferences("myPref", 0);
if (prefs.getBoolean("myBool", false)) { //myBool is true; }
sorry i will not post the question correctly
The problem is not with preference it returns correct value the problem is in handler which is used to slide the viewpager based on the value which is returned by preference
the handler is running even though the activity is killed below the code solved my problem
SharedPreferences spref = PreferenceManager.getDefaultSharedPreferences(this);
boolean isSlide=spref.getBoolean("SLIDE_SHOW",false);
if(isSlide==true)
{
SlideShowTimer();
}
public void SlideShowTimer()
{
mFilterTask = new Runnable() {
#Override
public void run() {
int i=0;
itemPosition = viewPager.getCurrentItem() + 1;
if(itemPosition!=10)
{
if(i <= adapter.getCount()-1)
{
viewPager.setCurrentItem(itemPosition,true);
mHandler.postDelayed(mFilterTask, 3000);
i++;
}
}
else
{
//ViewPagerAdapter.mediaPlayer.stop();
ViewPagerActivity.this.finish();
}
}
};
mHandler.removeCallbacks(mFilterTask);
mHandler.postDelayed(mFilterTask,3000);
}
#Override
public void onDestroy(){
super.onDestroy();
mHandler.removeCallbacks(mFilterTask);
}
Related
This question already has answers here:
Shared preferences for creating one time activity
(14 answers)
Closed 4 years ago.
How can I use SharedPreferences on Android Studio to save some data like the value of a boolean?
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE).edit();
editor.putBoolean("firststart",false);
editor.apply();
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE);
boolean firstStart= prefs.getBoolean("firststart",false);
if (!firstStart) {
Intent intent12 = new Intent(getApplicationContext(),FirstStart.class);
startActivity(intent12);
prefs.getBoolean("firststart",true);
}
else if (firstStart) {
}
If I use this code everytime I create the activity the value of the boolean return false and then true.
How can I resolve this problem and don't lose the data?
You do not need to save false as value everytime , simply if there is no value, you get false here prefs.getBoolean("firststart",false) otherwise true as your saved value
SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE).edit();
SharedPreferences prefs = getSharedPreferences(MY_PREFS_NAME,MODE_PRIVATE);
boolean firstStart= prefs.getBoolean("firststart",false);
if (!firstStart) {
// save true during first time initialization
Intent intent12 = new Intent(getApplicationContext(),FirstStart.class);
startActivity(intent12);
editor.putBoolean("firststart",true);
editor.apply();
} // for second run, when you get true
else if (firstStart) {
}
well actually your code is resetting itself on each onCreate so what you have to do is something like this
public class MyActivity extends Activity {
SharedPreferences prefs = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
}
#Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firststart", true)) {
// Do first run stuff here then set 'firstrun' as false
// using the following line to edit/commit prefs
prefs.edit().putBoolean("firststart", false).commit();
}
}
}
hope this helps
Let us presume that we have a function defined this way:
public class MyClass {
public static void RunFirstTime() {
//...
}
}
The very first time I install the app, I want to run RunFirstTime() but then any other time I run the app, that function should not run.
Is there a built-in way to do this?
Just use SharedPreferences and store a boolean variable when you are done with first run.
To be more clear, the code should be something like this:
public static void RunFirstTime() {
SharedPreferences prefs = getPreferences(MODE_PRIVATE);
boolean firstRun = sPref.getBoolean("firstRun", true);
if(firstRun){
//Do something here
....
//Save firstRun = false in order to not repeat next time
Editor prefEdit = sPref.edit();
prefEdit.putBoolean("firstRun", false);
prefEdit.commit();
}
}
Try code like this:
public boolean isFirstRun() {
SharedPreferences sPref;
sPref = getPreferences(MODE_PRIVATE);
boolean isFirstRun = sPref.getBoolean("FirstRun", true);
Editor ed = sPref.edit();
ed.putBoolean("FirstRun", false);
ed.commit();
return isFirstRun;
}
I have a Settings class so the user can decide to subscribe/unsubscribe to channels in Parse Push.
I think I got it all figure out except for the part to recover, and maintain the switch state next time user open the app or changes the state.
Can someone please help me on how to save the state, and switch the SWITCH to what the user selected?
public class Settings extends Activity {
/**
* Called when the activity is first created.
*/
private Switch krspush, egspush;
public static final String PREFS_NAME = "SwitchButton";
krspush = (Switch) findViewById(R.id.krspush);
egspush = (Switch) findViewById(R.id.egspush);
SharedPreferences sharedPrefs = getSharedPreferences("SwitchButton", MODE_PRIVATE);
// How?
public void onKrsClick (View view) {
boolean on = ((Switch) view).isChecked();
if (on) {
SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
editor.putBoolean("onKrsClick", true);
editor.commit();
ParsePush.subscribeInBackground("egersund");
} else {
SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
editor.putBoolean("onKrsClick", false);
editor.commit();
ParsePush.unsubscribeInBackground("egersund");
}
}
public void onEgsClick (View view) {
boolean on = ((Switch) view).isChecked();
if (on) {
SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
editor.putBoolean("onEgsClick", true);
editor.commit();
ParsePush.subscribeInBackground("egersund");
} else {
SharedPreferences.Editor editor = getSharedPreferences("SwitchButton", MODE_PRIVATE).edit();
editor.putBoolean("onEgsClick", false);
editor.commit();
ParsePush.unsubscribeInBackground("egersund");
}
}
Override the onCreate method of that activity class and attempt to load the values you saved in SharedPreferences.
krspush.setChecked(sharedPrefs.getBoolean("onKrsClick",false));
findviewbyid will crash unless called after the view is created ie, in the oncreate method.
Consider using click listener on your switches.
I don't see the point of this line of code "SharedPreferences sharedPrefs = getSharedPreferences("SwitchButton", MODE_PRIVATE)"
Here is how you use shared preferences : https://stackoverflow.com/a/23024962/2590252
You better look into some samples to learn about best coding practices http://developer.android.com/samples/index.html
I am developing an android application which should authenticate user only once in his mobile after installing the app. It should not ask the details for the second time. For this I have used shared preferences by setting a Boolean value. But it,s not working. Is there any suggestions here.. thanks friends !! My code is here
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("Register", true);
editor.commit();
use it this way :
in your onCreate() use :
if (isFirstTime()) {
// do what you want to do only once
}
to call the below :
private boolean isFirstTime()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
// first time
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
}
return !ranBefore;
}
Your approach of using SharedPreference is correct.Put the below coad snippet in your oncreate() method of the Activity.
SharedPreferences prefs=PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
register = prefs.getBoolean("Register",false);
if(!register)
{
//register user
SharedPreferences.Editor editor=prefs.edit();
editor.putBoolean("Register", register);
editor.commit();
}
else{
//continue with the rest
}
the first time :
settings.edit().putBoolean("Register", true).commit();
and in onCreate you do the test
if(!settings.getBoolean("Register",false))
//it isn't the first time
So the full code :
SharedPreferences settings = getSharedPreferences("settings", 0);
if(!settings.getBoolean("Register",false)){
//it isn't the first time
}
else{
settings.edit().putBoolean("Register", true).commit();
}
What I'm trying to do
Hello Guys.
I got a Service which, set's a boolean to true or to false whenever the service is started or stopped (started = true / stopped = false) in the SharedPreference. Now when I try to get the Boolean out there in my Activity, it allways dosn't find it. How can I solve this... Here's the Code for you Guys.
Code
Methode in my Service:
private void setStarted(boolean started) {
// SharedPreferences casten
mPrefs = this.getSharedPreferences(LOG_TAG, MODE_PRIVATE);
// Boolean in SharedPreferences hinzufügen
SharedPreferences.Editor editor = mPrefs.edit();
editor.clear().apply();
editor.putBoolean(PREF_STARTED, started).commit();
editor.commit();
//mPrefs.edit().putBoolean(PREF_STARTED, started).commit();
Log.d(LOG_TAG, "Variabel " + mPrefs.getBoolean(PREF_STARTED, false));
}
In my Activity
// mPrefs caten
mPrefs = this.getSharedPreferences(GPSService.LOG_TAG, MODE_PRIVATE);
// boolean holen ob service gestartet oder nicht
run = mPrefs.getBoolean(GPSService.PREF_STARTED, false);
How do I get the boolean out of there? It allways returns me the default value I had to give in the getBoolean Methode.
Thanks for your help in advance
safari
Here is some code that I'm successfully using in one of my apps. It is used in various parts of the app, e.g. both from activities and services:
void putValue(Context context, String pref, boolean value) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putBoolean(pref, value);
editor.commit();
}
boolean getValue(Context context, String value, boolean defaultValue) {
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, Activity.MODE_PRIVATE);
return settings.getBoolean(value, defaultValue);
}
Try using getDefaultSharedPreferences(Context context) method of PreferenceManager in both your services and your activities.
private void setStarted(boolean started) {
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
mPrefs.edit().putBoolean(PREF_STARTED, started).commit();
Log.d(LOG_TAG, "Variabel " + mPrefs.getBoolean(PREF_STARTED, false));
}
In your Activity
mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
run = mPrefs.getBoolean(GPSService.PREF_STARTED, false);
Also make sure you :
Never call .clear() on editor.
You use PreferenceManager.getDefaultSharedPreferences(this) everywhere.
Just build your prefs object the same way you would as part of your activity. The only difference is pulling your application's context in.
SharedPreferences prefs = _ getApplicationContext().getSharedPreferences("com.example.appname", Activity.MODE_PRIVATE);
Boolean tmpBool = prefs.getBoolean("PREF_NAME", null);