How to store a boolean value using SharedPreferences in Android?
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); // getActivity() for Fragment
Boolean statusLocked = prefs.edit().putBoolean("locked", true).commit();
if you dont care about the return value (status) then you should use .apply() which is faster because its asynchronous.
prefs.edit().putBoolean("locked", true).apply();
to get them back use
Boolean yourLocked = prefs.getBoolean("locked", false);
while false is the default value when it fails or is not set
In your code it would look like this:
boolean locked = true;
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences();
if (locked) {
//maybe you want to check it by getting the sharedpreferences. Use this instead if (locked)
// if (prefs.getBoolean("locked", locked) {
prefs.edit().putBoolean("locked", true).commit();
} else {
startActivity(new Intent(parent.getContext(), Tag1.class));
}
How to save the Boolean value in sharedPreference also save the imageView
This is how you store a boolean in shared preference:
SharedPreferences prefs= getSharedPreferences("sharedPrefName", Context.MODE_PRIVATE);
SharedPreferences.Editor editor= prefs.edit();
editor.putBoolean("lockedState", yourBooleanVariable);
editor.apply();
This is how to access it again somewhere:
SharedPreferences prefs = getSharedPreferences("sharedPrefName", Context.MODE_PRIVATE);
boolean bool= prefs.getBoolean("lockedState", null);
And what do you mean by value of img? Please Explain?
android sharedPreferences to set and retrieve boolean value
This is how you need to work with shared preferences.
The senerio I am about to show is to check if this particular activity is running for the first time or not.
Here is what you do in your on create method:
//SharePrefernces
SharedPreferences appIntro = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
//Check if the Application is Running for the First time
appIntro = getSharedPreferences("hasRunBefore_appIntro", 0); //load the preferences
Boolean hasRun = appIntro.getBoolean("hasRun_appIntro", false); //see if it's run before, default no
//If FirstTime
if (!hasRun) {
//code for if this is the first time the application is Running
//Display Activity
super.onCreate(savedInstanceState);
setContentView(R.layout.app_intro_activity);
//Button to send Confirmation back
Button btn_ok = (Button) findViewById(R.id.appintro_btn_ok);
btn_ok.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
//Save information that this application has run for the first time
SharedPreferences settings = getSharedPreferences("hasRunBefore_appIntro", 0);
SharedPreferences.Editor edit = settings.edit();
edit.putBoolean("hasRun_appIntro", true);
edit.commit(); //apply
Intent intent = new Intent(Application_Intro_Activity.this, MainActivity.class);
startActivity(intent);
//close Activity
finish();
}
});
}//End of if(firstTime)
else {
//code if the Application has run before
super.onCreate(savedInstanceState);
//Navigate Directly to MainActivity
Intent intent = new Intent(Application_Intro_Activity.this, MainActivity.class);
startActivity(intent);
//close Activity
finish();
}
}//End of OnCreate()
Pass and retrieve boolean value from multiple activities using sharedPreference
You are not set them false in any condition that's why all if blocks are executed. initialize these value to false by default then set true depend on your condition.
highRiskOfHeartDisease = true;
lowRiskOfHeartDisease = true;
unlikelyNoHeartDisease = true;
other = true
Related Topics
Android Mediaplayer Throwing "Prepare Failed.: Status=0X1" on 2.1, Works on 2.2
Gradle Dsl Method Not Found: 'Compile()'
Add Button to a Layout Programmatically
Android Actionbar How to Add Supporting Library V7 Appcompat for Eclipse
How to Parse Same Name Tag in Android Xml Dom Parsing
How to Simulate Android Killing My Process
Android: Background Image Size (In Pixel) Which Support All Devices
Which Websocket Library to Use in Android App
Centering Recyclerview Items with Flexboxlayoutmanager
Answer Incoming Call Using Android.Telecom and Incallservice
Supporting Multiple Screen Size - Android
Android Device Manager Fails to Launch After Updating to MACos Big Sur 11.3
How to Apply Zoom, Drag and Rotation to an Image in Android
Why Does My App Size on Device Differ Than the APK or Play Store Size
Closing the Database in a Contentprovider
Adding Opencv to Native C Code Through Cmake on Android Studio