Android beginner question pre-existing simple default preferences - android

have been away from Android for more than a year, trying to pick up on it again, but confused about having a simple default preference.
My app relies heavily on a SQLite3 db, which has hundreds of tables. The launch activity has to build and load a good amount of data at launch.
I just need a simple way to read two strings from a preferences file. The thing is that I want to have, the very first time the application opens, two default string values.
If the user changes that I will save back to it.
Been reading two books, SharedPreferences, File I/O, etc. just that all the examples seem a tad complicated for what I need.
So, If I create a res/pref/preferences.xml all I have seen around are PreferenceScreens as the root element
For my need, I think I just need a root preference such as:
<?xml version="1.0" encoding="utf-8"?>
<Preference
xmlns:android="http://schemas.android.com/apk/res/android">
</Preference>
how do I add two simple string key/value pairs that I would read at the very first launch, and only when needed write to it?
any help is appreciated, sorry for the noob'iness

I think you're over complicating what you need to do, sharedpreferences are perfect for this situation, and easily implemented. Just do the check in the onCreate() when the app starts :)
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//"Hello World" is the name of your preferences
SharedPreferences settings = getSharedPreferences("HelloWorld", 0);
//to get a value from pref - (NAME_OF_PREF,RETURN_IF_NOT_FOUND)
boolean enteredDetails = settings.getBoolean("details", false);
string foo = settings.getString("foo_name","no name");
//to write to them - (NAME_OF_PREF,VALUE)
SharedPreferences.Editor editor = settings.edit();
editor.putString("foo_name", "Joe Bloggs");
editor.putBoolean("details", true);
editor.commit();
}

Related

How to save number of times a user has tapped on a view

I am designing a simple application that will count how many times a user has tapped on a imageView. My question is what would be the best way of saving and reading this file. Any suggestions? I am thinking something like using Parse.com's local database. I have tried it, but I could not get it working the way I wanted. I am still a beginner, so please not so fancy suggestions.
Try to save data in SharedPreference. SharedPreference works like database for application on device that will be stored until any one has unistall app from device.
To create sharedPrefernce-
SharedPreferences prefs = this.getSharedPreferences(
"com.example.app", Context.MODE_PRIVATE);
To store data -
prefs.edit().putInt("key", int_value).apply();
To retrieve data-
// use a default value
int l = prefs.getLong("key", default_value);
Simplest options is always thebest option, go with shared preferences
Here is simple tutorial from google http://developer.android.com/training/basics/data-storage/shared-preferences.html
It will store your data in application local file. Take a note of that there are different shared preferences in example getPreferences() will return file specific for activity you used this method. While getSharedPreferences() will return application global file.

Android: Using self-defined metadata

I want to make an Android application which shows questions on the go on the basis of user selection. But I won't use a server, and so the questions have to be bundled with the app. But adding the whole questions would not be a great design, so either SQLite database can be used, or xml metadata can be used. But SQlite bundling I heard makes the app large in size. Is that so? And could someone explain how to refer to a xml file with self-defined metadata, to create questions on the fly. What will be the best way to do this?
the SQLite DB is on the phone already, I've used it a few times with no big jump in .apk size.
If you are looking for an easy-to-use stored hashmap, try SharedPreferences! Though I wouldn't use it for a heavy solution, the implementation guarantees ACID and is very straightforward. you can make several different hashmaps and name them different things with SharedPreferences http://developer.android.com/reference/android/content/SharedPreferences.html
I declare:
private SharedPreferences anchorHash;
in onCreate:
anchorHash = getSharedPreferences(getString(R.string.anchor_hash), MODE_PRIVATE);
inonPause:
SharedPreferences.Editor editor = anchorHash.edit();
editor.clear();
for( String s: anchors.keySet()){
//it's a another hashMap I want to write here, but you can do this however you like
editor.putString(s, anchors.get(s));
}
editor.apply();
//or commit() if you need to know about the success ( it'll happen )

Saving Shared Prefrences in Multiple instances

I would like to be able to save my users session or sharedPrefrences in a way that if the user kills the application and you start it it would look like this.
Button one = Start Activity with Blank Preferences
Button Two = List of Saved Sessions of Preferences and once clicked all put into the Starting activity.
Is this possible and if so how would I go about doing that?
Thank you!
Yes you can do that and it is good to use sharedPreferences if you just have to store some session variables. But if it is more, then go for database.
Do clear sharedPrefences in your application you need to do this:
SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(activity);
Editor editor = settings.edit();
editor.clear();
editor.commit();
For reading the preferences, you can keep a sharedPreference with the count for the seesions. While saveing the prefences, always save with the strings session1, session2, session3 etc. So, while accessing them based on count, prepare a loop and form the string and access all the session variables and show them.
The reason why I didnt suggest you to do getAll() for sharedPreference is that, you may save few other things in sharedPreference. So by forming strings yourself, while reading you can just get the sessions and not other data saved in sharedPreference.
I hope you understand what I meant
Is this possible
I would say yes, depending on exactly what you mean.
if so how would I go about doing that?
SharedPreferences has a couple different functions to do something like this, depending on exactly what you want. You can get a Map of all preferences that are stored after clicking Button2 with getAll() or a set of preferences with a certain String such as "userName" or something similar with getStringSet(). Play around with the functions it offers and see if it gives you what you are looking for.
Also take not of the warnings of these functions
Note that you must not modify the set instance returned by this call. The consistency of the stored data is not guaranteed if you do, nor is your ability to modify the instance at all.

Writing to shared preferences too slow?

I have created a service that writes some information about a widget once a user places it on home screen(the info is picked up from the confutation activity)..i also write down the number of widgets the user has set up.
Once the user removes the widget i delete that info in the shared preferences.
What i have experienced is that if user places for example 2 widgets, then removes one, then places one again, doing all those actions fast, the shared preferences file gets inconsistent values in it. Sometimes it works ok but most of the time i get stuck with wrong values in it.
I am using apply(), i've tried with commit but same thing happens.
The values i store in the shared preferences are crucial for the system to work, without it the widgets are useless since they are backed up by info from internet based on the user configuration which is written in preferences.
Is switching to a database solution more reliable or any other viable solution which will fix this "race condition"? (maybe forcing my own mechanism of synchronization, but as far as i've understood from docs, apply() is already synchronized, and the read/write should first go to RAM which should make it fast and i shouldnt be experiencing any problems like this since the user cant physically manage to delete a widget and place a new one faster then 2-3 seconds top!)
Try using the synchronized keyword in working with the SharedPreferences itself. For example, here is a method that could be used when setting an application String in the SharedPreferences of an Android app:
public synchronized static void setAppString(Context context, String pref,
String val) {
SharedPreferences sp = context.getSharedPreferences(
APP_PREFS_UNIQUE_ID, Context.MODE_PRIVATE);
Editor editor = sp.edit();
editor.putString(pref, val);
editor.commit();
}
For few/simple key-value pairs, you might not need the overhead of a database paradigm.

How to attach a Preference to a particular SharedPreferences?

I am working on implementing the preferences for our application. I know how to display preferences UI and how to read/write values using SharedPreferences. In our app, I need to handle two sets of preferences and I would like to ask about this issue, one comment in the Android documents in particular.
The documentation for Preference.getSharedPreferences() has the following comment under the Return values section:
Returns The SharedPreferences where this Preference reads its value(s), or null if it isn't attached to a Preference hierarchy.
I would like to ask how it is possible to attach a SharedPreferences to a particular Preference, be it EditTextPreference or others. In other words, how does the persistence code in a Preference know that it should store the user input in one particular SharedPreferences object and not the other?
To explain my question further with an example, suppose I have the following:
SharedPreferences prefs1 = getSharedPreferences(file1, mode);
SharedPreferences prefs2 = getSharedPreferences(file2, mode);
My question is what API I should use so that prefs1 is used by the Preference objects' persistence code and not prefs2.
The target is Nexus One, running 2.3.4.
Maybe the answer is obvious but I could not find it after reading the documentation and searching the web. Thank you in advance for your help.
In other words, how does the persistence code in a Preference know that it should store the user input in one particular SharedPreferences object and not the other?
Preference uses PreferenceManager's getSharedPreferences(), which eventually routes to getDefaultSharedPreferences().
You are welcome to create your own Preference subclasses that change this behavior, but since the preference screen system may not be designed to handle multiple SharedPreference objects, there's a chance that your preference changes might not get persisted.
IOW, I encourage you to reconsider:
In our app, I need to handle two sets of preferences

Categories

Resources