I have a button in a customView which has several numbers on it individually,it counts backward
now my program doesn't keep value on it when user taps back and closes the program How can I prevent this ?
You can pass value from one activity to other using intents. If you want to persist data,the simplest way to achieve this would be saving it in Shared Preferences.
Developer link has good information on how to implement this -
You can save the value Key, Value pair and then retrieve it when the app is up again.
you can Use SharedPreferences its easy to write and get.
You can create one like this
SharedPreferences prefs = getSharedPreferences("yours", MODE_PRIVATE);
and write to like this
SharedPreferences.Editor prefsEditor=prefs.edit();
prefsEditor.putString("what you want", "value of what you want");
prefsEditor.apply();
and retrive like this
String s=prefs.getString("what you want", "value to be retrived if failur happens in retreiving")
if the value not important after end the program you can save it into public static variable
but if you need the variable after end the program you can use SharedPreferences
follow this tutorial http://developer.android.com/reference/android/content/SharedPreferences.html
Related
I have a school planer app and I want to save the classes that are written in an EditText object. The problem is, the EditTexts are empty when I close the app and then open it again, and even when I press back. This may be a noob question, but I'm a beginner and I need help. Please answer quickly...
for saving edittext text you need to store the text into storage.
android support storage like
Database
Shared Preferences
For your problem you need to store your Edittext text into the storage when you change value.
And when you open it again you need to retrieve from the storage.
For you best option will be shared preferences. For more Go with This Example . This will make you understand save and retrieve shared preferences.
Short Example
Store Data
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
editor = pref.edit();
editor.putString("first_edittext", "value");
editor.commit();
Retrieve Data
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
pref.getString("first_edittext", "");
I Am writing a code using SharedPreference to store username and password of a user but each time I entered information the older one in xml file are override by newer one what I have to to to get all my data?
SharedPreferences sp1=getSharedPreferences("myshared", 0);
sp1.edit().putString("name", name.getText().toString()).commit();
sp1.edit().putString("pass", pass.getText().toString()).commit();
sp1.edit().putString("age",age.getText().toString()).commit();
sp1.edit().putString("id",id.getText().toString()).commit();
It sounds like you're overwriting your shared preferences between activities. SharedPreferences are persistent like a file on disk, so you shouldn't ever have an issue with the values not being set, hence why you must be overwriting it.
You can get your SharedPreferences by doing
SharedPreferences sp1=getSharedPreferences("myshared", 0);
String name = sp1.getString("name", "noname");
String pass = sp1.getString("pass", "nopass");
...
You can determine if the name/pass was set by checking if they equal the default value (noname and nopass in this case, though it could easily be null).
I am developing a android application where I need time stamp should be saved in time picker even if we click back button in android.Since I am new to android I need help.Thanks in advance for help.
System.currentTimeMillis();
Will get you the current timestamp.If you want to save it somehow you may use SharedPrefences or sqlite db or something like this.
I think I would prefer SharedPreferences since you want the timestamp to be there after clicking the back button. Clicking that button might exit you application so some persistent storage would be fine. The timestampt is no POJO so SharePreferences are my first choice.
Something like that should help:
// That will save the timestamp
SharedPreferences pref = context.getSharedPreferences("PREF_TAG_TIMESTAMP", Context.MODE_PRIVATE);
pref.edit().putLong("PREF_TAG_TIMESTMAP", System.currentTimeMillis()).commit();
pref.edit().clear().commit();
// That will get you the timestamp
SharedPreferences pref = context.getSharedPreferences("PREF_TAG_TIMESTAMP", Context.MODE_PRIVATE);
pref.getLong("PREF_TAG_TIMESTMAP", -1.0);
If you need to persist you data for a later run of the activity you can do that using the sql database, shared preferences or if you only need to persist during the current run of the app (not for subsequent launched of the app) you could create a static variable holding the values you want to store for you.
From what I understand you should go with the static variable approach.
i am new to android, i searched a lot and couldn't find a satisfying answer; what i need is to save some setting for my application such as
1> language, number of items to display, display/not display images, etc...
which i think is best done using the shared preferences
2> save which data categories to get from the internet...
here is my problem:
i have data divided into category objects with key, name, type, data[]...(data[] is changing all the time and not saved after exiting the application),(key, name, type are final values defined by programmer).
and because there are many categories which the user may or may not want to load(around 25), he/she can choose which categories to display, and these choices must be saved.
i think using shared preferences will not help because of the complexity of the data; i was thinking of using sqlite or xml, not sure which is the best choice keeping in mind efficiency and memory size.
note: i am using a global variable for the category information array, that is because data[] needs to be refreshed automatically every 2-3 minutes and must be available to all activities also efficiency and memory space are an issue.
i will appreciate any advice, thank you in advance.
1> SharedPreferences is the right choice
2> you have multiple possibilities
a) Use the internal storage and use Object Serialization (for simplicity reasons), xml (if you want to exchange the data) or use a own format
b) SQLite is the fastest solution. But you have to do more programming for that
b would be my choice, so create a DB-Object (static or singleton pattern) and write functions for every database task
1) Yes, SharedSettings should help youy. There is plenty information around, but post back if you are lost.
2) I would make a table with all the categories and a boolean value thats says "show" where you can keep if the user wants to show it or not.
Of course it depends on the size of the categories and how much they change, because otherwise you will be updating the database forever.
User shared preference in store your setting,
SharedPreferences sharedPreferences = this.applicationContext.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
//For saving the setting:
//for storing long
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putLong(key, value);
//for storing string editor.commit();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
//and similarly for int,float etc
//For Retrieving the string:
sharedPreferences.getLong(key, defaultValue); //for long value
sharedPreferences.getSting(key, defaultValue); //for string value
I've been looking for over the past day and a half at several websites about how to store strings/string arrays/etc. and I can't seem to figure it out. I'm not sure if I'm just not quite comprehending how to implement data storage..or what. But here is my problem, simply put, I have two activity pages (we'll name 'A,B' respectively) . All I want to do is get a string from a text view in Activity B, store it in an array, and then have it accessed by clicking a button in Activity A.
I know it is simple, but I hit a block for some reason... I am trying to use SharedPreferences but how would I obtain the string from Activity B, store it in a global array, and let it be accessed by a different activity (Activity A) ?
Just store it into shared preferences (usually in onPause()):
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putString(GAME_STATE, writer.toString());
editor.commit();
in one activity and load in another (usually in onResume()):
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
String jsonState = preferences.getString(GAME_STATE, null);
And nothing prevents you from using public static variables
You can't store an array directly in shared preferences, but you can store a set.
See putStringSet and getStringSet. You can add all the items from your array to a LinkedHashSet (as long as they are unique) if you wish to store them in SharedPreferences. See Konstantin's answer for the general idea on how to use SharedPreferences.
Yes you can obtain and store value in global array but you will loss everything once you close the application. So its better you create a table and store/get values from the table whenever you required.
Here is an example for creating SQLite example.