I want to clear the value of the shared preference - android

I have stored the username and the password in the sharedpreference.
And I am displaying the username in every activity like Welcome "Username".
But at the time of logout I have put one checkbox in the dialog box.If the check box is checked the sharedpreference value should be clear. So I don't know how to do it.Please help me out of it. Thank you.

SharedPreferences settings = getSharedPreferences("MyPreferences", 0);
if (settings.contains("mykey")) {
SharedPreferences.Editor editor = settings.edit();
editor.remove("mykey");
editor.apply();
}

The method to clear the sharedpreferences is this
http://developer.android.com/reference/android/content/SharedPreferences.Editor.html#clear()
With this you dont delete the xml
Editor.clear();
Editor.commit();

You have to use remove method which is simple and described here. The only parameter is the Key you have used to save this preference.

1st method
Your_sharedprefrence_name..clear();
Your_sharedprefrence_name.commit();
2nd Method
Your_sharedprefrence_name.clear().commit();
3rd Method(When u want to clear the arraylist of sharedprefrence put it in loop)
Your_sharedprefrence_name.remove(String.valueOf(i)).clear().commit();

Related

How to remove Specific values from Shared Preference

Am using SharedPreferences to store list of values. What I need is to remove specific value from SharedPreferences.Below is my code am using to remove. But its not working.
prefs= DetailActivity.this.getSharedPreferences("itemFKID",Context.MODE_PRIVATE);
edit=prefs.edit();
//edit.clear();
edit.remove(itemFkId);
edit.commit();
Below is Screenshot that contains values even after edit.remove() compiles.
Here am inserting values into SharedPreferences
prefs= DetailActivity.this.getSharedPreferences("itemFKID",Context.MODE_PRIVATE);
edit=prefs.edit();
for (int i = 0; i < Config.favouritesList.size(); i++) {
edit.putString("itemFKIDValue" +i, Config.favouritesList.get(i));
}
edit.putInt("itemFKIDLength", Config.favouritesList.size());
edit.commit();
The documentation for SharedPreferences.Editor has two bits that are relevant to your question:
All changes you make in an editor are batched, and not copied back to the original SharedPreferences until you call commit() or apply()
And
when committing back to the preferences, all removals are done first, regardless of whether you called remove before or after put methods on this editor
So you'll have to step over the commit() call before you see the value removed.
Finally found the mistake. Key passed in remove() is wrong. Instead of edit.remove(itemFKIDValue) I have used edit.remove(itemFkID). Thanks for the time guys.

SharedPreferences not writing / reading

We made a simple clicker game and I wanted to save the highscores in the SharedPreferences. So we wrote this code in MainActivity:
SharedPreferences sharedPref = getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("Highscore",clicks);
editor.commit();
int clicks is the score you made that round.
In another activity, we wanted to display the Highscore in a TextView:
SharedPreferences sharedPrefs = getSharedPreferences("myPrefs",Context.MODE_PRIVATE);
highscore = sharedPrefs.getInt("Highscore",0);
highscoretv.setText(Integer.toString(highscore));
But the highscore wasn't displayed. Have you an idea what I can do??????
Maybe try to replace Context.MODE_PRIVATE to just MODE_PRIVATE in both activities
You should use this piece of code instead of Context.MODE_PRIVATE
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Instead of using editor.commit(); , you should use editor.apply();
Apply will update the preference object instantly and will save the new values asynchronously.
Sorry guys. I solved it by myself. The Highscore was displayed in the Launcher Activity that was already open in the background. So as I closed the MainActivity, the LauncherActivity got in Foreground, but didn't update. Can you tell me if theres a thing like onActivityinForeground so I can prevent this?

How to display the server response on textview that should be displayed on all the screens in Header in android?

I want to display the server response on multiple screen in header bar in Android? Please, tell me how to do that? I created an abstract class and defined the Asynctask class. In onPostExecute method i am using the textview for displaying the result. Now my question is how all the activities access this textview?
I am new with Android. Please, give me the proper way to solve this?
May be you can use the shared preference feature of android to save the response.
References are How to use SharedPreferences in Android to store, fetch and edit values and http://www.vogella.com/tutorials/AndroidFileBasedPersistence/article.html
On each activity, you can set the header by referring the value from shared preference.
Create one base activity and extend that created base activity instead of activity. Create one xml file in base activity, in it only declare your header. nothing else, now to display use that xml and for the other contents you can use your actual xml file. So this one is for to set global header. Now, save your response to Shared preferences and use it throughout the app whenever you need.
Store the value into Shared preferences
SharedPreferences sharedPref = context.getSharedPreferences("app_prefs", Context.MODE_PRIVATE);
// saving
sharedPref.edit().putString("key", "value").commit();
// reading
sharedPref.getString("key", "default");
You need to store the response values in Stringbuffer
StringBuffer value =new StringBuffer("your response value");
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("Identifier",value);
editor.commit(); //important, otherwise it wouldn't save.
Get the value in next activity
SharedPreferences prefs = getSharedPreferences(PREFS_NAME, 0);
final String myVariable = prefs.getString("Identifier", "value");

How to pre populate data between the screens in android

I have an app with 3 screens.
For each screens i have next button in each screen to go to the next screen.
problem:
Scenario 1:
--> I filled the data in first screen
--> I go to the second screen.
--> When i come back to the first page i am able to see the data what i filled.
-->But when i again go to the second page i am not able to see the data what i filled in the second form.
How can i manage this in android?
Thanks in advance...
You can save the data in Shared preferences. Check this : Shared Preferences Android and Example
Hope this helps.
You need to save the state yourself using your activities' lifecycle methods. Read this: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
You can save temporary data in application data. Once in the onCreate method, you can reuse that data, or you can use an activity flag to ensure that there's only one instance of your activity in the backstack.
try
{
//in first run app. go to catch and in other go to try
SharedPreferences pref = getSharedPreferences("pref",0);
SharedPreferences.Editor edit = pref.edit();
String x= pref.getString("login", null);
edit.commit();
if(x.equals("first"))
{
//here you can fill editbox by value you entered before
edt1.setText(x);
//and make this way to all edittext
}
}
catch(Exception e)
{
SharedPreferences pref = getSharedPreferences("pref",0);
SharedPreferences.Editor edit = pref.edit();
edit.putString("login",edt1.getText().toString());
//edt1.getText().toString() value(s) you want to save
edit.commit();
Intent intent = new Intent(getApplicationContext(), First.class);
startActivity(intent);
}

checkbox value in other activity

hy, everyone
i'm searching for a few days a solution for one problem i tried to search but i didn' find an answer for my problem so i decided to ask you maybe someone it will so kind and wil explaine to me ho can solve my problem.
So i have two activities, a configure one and a main one, my problem is that when i check one checkbox in the configure activity (i succeed to save the checkbox state so when i check it will be checked even if i leave the configure activity), but i don't know how can i use the checkbx state in the main activity. i found many suggestions but for for me nothing worked.
thank you in advance.
thank you for all for the answers but i not succeeded in this way to solve my problem, and with your suggestions i did the following: in the second activity(where i have the CheckBox): i have one Save function where i save with SharedPreferences the Checkbox State( even if i close the activity or anything else the state of Checkbox it's keeping his state) and i have made a Load function where when i load back the activity it's loading the previous state so inside this activity i thing it is ok, But in this case how can use the load function's value (in fact the checkbox value) int the MainActivity.
I'm sorry for inconvenience but i'm knew but i would like to learn.
Thank you ones again.
Store the value in SharedPreference to get the value of checkbox
chkIos.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (((CheckBox) v).isChecked()) {
SharedPreferences sharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
Editor editor = sharedPreferences.edit();
editor.putString("checkboxState",((CheckBox) v).isChecked());
editor.commit();
//Save the state of checkbox in sharedPreference
}
}
});
In main activity get the state like this.
SharedPreferences sharedPreferences = context.getSharedPreferences("settings", Context.MODE_PRIVATE);
String stateValue = sharedPreferences.getString("checkboxState);
i guess the sharepreferences will resolve that
SharedPreferences.Editor editor = app_preferences.edit();
editor.putBoolean("Checkbox", value);
editor.commit();
SharedPreferences app_preferences =
PreferenceManager.getDefaultSharedPreferences(this);
boolean value = app_preferences.getBoolean("Checkbox", false);
You can use SharedPreferences to save the state of your CheckBox in the device so you can retrieve it from another Activity.
Here is an example how to use them. Also you can check this YouTube video that shows you how to make preferences screen for your Activities.

Categories

Resources