Keeping data when Android does an onExit() - android

I have a String array that I want to keep to display in a ListView, but every time I exit the activity, the global String array is deleted and the ListView doesn't display. Is there anyway that I can keep a String array in the application's storage for the next use?

It's actually pretty easy for just a string. You can store it in shared preferences. See this link: http://developer.android.com/guide/topics/data/data-storage.html#pref

Related

Android Saving Information To Device

I'm making an Android application and want to create a "Favorites" list for some objects in the app. I wanna make the list accessible and editable in all my activities and I can't really figure out the best way to do this.
Shared preferences? Writing a small txt file to the device? What's the fastest way to do this?
Thanks in advance!
dependencies {
compile 'com.google.code.gson:gson:2.3.1'
}
Then when you want to save, convert your array into String:
ArrayList<Type> yourData = new ArrayList<Type>();
String dataStr = new Gson().toJson(yourData);
//put this dataStr in your shared preferences as String
To retrieve and convert back to an object is also simple:
String str = "";
//you need to retrieve this string from shared preferences.
Type type = new TypeToken<ArrayList<Type>>() { }.getType();
ArrayList<Type> restoreData = new Gson().fromJson(str, type);
If you want to create a Favorites list, use a SQLite Database.
There's really only four ways to store data.
Shared Preferences
Databases
Local files
Remote Server - Slowest since it depends on network connection, so let's skip this.
Between the remaining 3, SharedPreferences is a great option when used to store a single value. However, it's not a good option for storing a Favorites list, mainly because you can only store a single String.
You can still store it by combining all items in your list into one string, then splitting it each time. However, as your Favorites list gets larger, this single long String will too. Splitting and combining all the time isn't efficient.
SharedPreferences is still a decent option if you only have to store the Favorite's list, but since you want to edit it too, it becomes a less attractive solution.
Local Files and Databases are the better options, however local files require you to read in the file each time you want to use it. This process of reading and writing to a file isn't as efficient as using a Database, especially if you want to edit. For example, let's say you want to remove an item from the middle of your Favorite's list. This would require you to read in the file, edit it, then write the change into the file again. Not too pleasant when compared with the ease of the final solution.
Databases are the best option for this, mainly because it's designed to manage data. You can create a single Favorite's table and add each item as it's own individual row. Fetching the entire table becomes quick and easy. Fetching a single item becomes quick and easy. Adding a new item or removing a new item is also quick and easy.

How to save RecyclerView content

I'm using a RecyclerView as a checklist that can be used as a grocery list for example, first the user adds the product(item), the price and the store, then my app adds this item object into my ArrayList adapter; each item in my adapter is a relative layout with 3 TextViews for the product, price and store. The problem occurs when the user shuts down his device or closes the app: the RecyclerView resets to nothing.
What is the easiest way to save items in a RecyclerView so that it stays? Do I save in the cache?
Shared Preferences
or
SQLite Databases
You will need to save the data to persistent storage in some way. For something small, it would be best to use SharedPreferences. Then, when you re-open the app, load the data from SharedPreferences into your ArrayList, then populate the RecyclerView.
To use SharedPreferences you will need to convert your data into a String, because SharedPreferences can only handle primitive data types. The best way to convert lots of data to a String is by converting the data into a JSONObject, then that JSONObject into a String (which can be stored in SharedPreferences).
JSON is like a dictionary, it's a way to store Key-Value pairs. See this SO question for more information and some examples of what it looks like. Then search for how to use JSON and Android (or reference the docs).
EDIT: If your RecyclerView is showing a simple amount of data, you may be able to store a Set<String> in your SharedPreferences. See this relevant documentation.

How to store values in preferences over one another

When i write anything into shared preferences it overwrites the previous value, how can i continuously inject values in preferences and fetch it out all at once like an array? I do not want to use database for this operation.
Thanks
You can use JSON as #CommonsWare points out to store your data in SharedPreferences. I have such an answer that using GSON here:
https://stackoverflow.com/a/5968562/617044
Though not ideal, if you want to use Preferences only, You can store String in preferences. When next value is to be added, retrive the old one add 'comma' and new value.
Whenever you want to use these values, use StringTokeniser. Tokenise elements by 'comma' character.

How to reatain Values of list like we do for strings and int using shared preference?

I have one listView in my app, which i is use to list the differnt strings by dynamically adding the strings to ArrayList and using that in list view. I have declared this as below
public static List<String> myList = new ArrayList<String>();
this helps me in retaining the values of list even after exiting app, but i cant be able to retain it after i force close the app or cell boots off and on. Is there any way to store this list as we do in shared preference for strings and int?
Couple of methods.
1) Store the entire list as a Single String (delimiter separated) and split it when you read it back (depends on what kind of strings, because if you have delimiters inside the string, you're screwed, although you could use some kinda encoding (Base64))
2) Store them as separate variables "Key0" is the first element, "Key1" is the second...etc You get the idea...Remember to flush the Keys each time you save though.
3) Don't use Shared Preference, instead use a private file and write the entire ArrayList Object (It's serializable.) using a ObjectOutputStream
Cheers.

Android storing string array

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.

Categories

Resources