In my application I have to keep some setting options for a user, like currency type, language, Calendar view (Either Calendar or List) etc.
Here I am confused whether I have to configure all this by creating database tables or should keep this in any xlm or text file. Please guide if there any more convenient way? thanks.
you can use android shared preferences to store a users preferences. Alternatively you can store "global" variables if you create a Application instance which has static or instance variables to access (but these values can be lost if your Activity is destroyed)
The answer on this thread has details on global application to store application wide variables
Hope that helps
Android has Shared Preference specifically for this purpose.
http://developer.android.com/reference/android/content/SharedPreferences.html
The solution would be to use shared preference
You can also look into PreferenceActivity which helps in building a setting page.
Related
I am new to Android. I am building a test application where I need to store a variable's value say, strength of the user. I want to increase or decrease this strength's value whenever user uses the app. And after user closes the app and reopens it on next day, he should find the same value of strength.
One way I can think of is to store a local db in phone and read/write each time into that, since there are hardly 3 to 4 such variables. So db is not a good option I guess.
Other one I thought was to use android.app.Application class but I am not able to get what I want from that. Can we actually do it using android.app.Application? Or then any other method for 3 to 4 variables.
You can use SharedPreferences to store your variable inside shared preferences. For example, set it like:
SharedPreferences sharedPreferences = context.getSharedPreferences("DATA",Context.MODE_PRIVATE);
sharedPreferences.edit().putString("STRENGTH",yourVar).apply();
Then get it out using:
SharedPreferences sharedPreferences = context.getSharedPreferences("DATA",Context.MODE_PRIVATE);
strength = sharedPreferences.getString("STRENGTH",null);
Use SharedPreferences. SharedPreference provides an easy mechanism to persist a value across the life of an app.
Write an XML file in the application directory that the application can read on startup.
Use application class when our app is open read last state and store your data in shared preference, and you are right db is not good choice.
Like you suggested, you can use an SQLite database to store a table regarding the users. Although, you might only have one variable, strength, associated with each user now but if there's potential for more associated values that you want to store regarding the user, I'll recommend using an SQLite database.
Another option you can use is SharedPreferences, it allows you to store key value pairs. So in your case, each strength value can correspond to a username.
If you haven't learned the different storage options for Android, I'll recommend you taking a look at this.
Hope this helps!
I am new to this concept ,How to display my fragment after in application,that fragment only display at after installing my app, please any one suggest me how to do that
Basically you need to store a value outside of the application itself. Shared Preferences or a database (eg SQLite) or a file could be used. The former, Shared Preferences, would be the simplest. There are loads of tutorials and examples eg Android Shared preferences example
You could use a boolean type or even simply check for the existence of any type and then set or create it respectively.
I find the name of stored preferences on Android (SharedPreferences) to be a tad confusing. It was probably chosen with a purpose, so how exactly are these preferences shared? e.g. I don't want other applications accessing my app's information willy-nilly.
What is the difference between preferences from getPreferences() and preferences from getSharedPreferences(), and what is the difference between the different modes? How should I choose which to use? (Heck, if I have a multi-activity program, will using just the regular getPreferences be detrimental?)
My imagination regarding the use of these things is still pretty limited.
It was probably chosen with a purpose, so how exactly are these preferences shared?
They are shared among all components of your application (e.g., all of your activities).
e.g. I don't want other applications accessing my app's information willy-nilly.
SharedPreferences are private to your application by default.
What is the difference between preferences from getPreferences() and preferences from getSharedPreferences(), and what is the difference between the different modes? How should I choose which to use? (Heck, if I have a multi-activity program, will using just the regular getPreferences be detrimental?)
I am not quite certain what you are referring to, since you provided just bare method names with no classes. If you are intending on collecting preference values from the user via the preference screen system, use PreferenceManager.getDefaultSharedPreferences(). I generally use that for everything.
Sharedprefernce are basically key,value pairs with can are saved in an xml and is accessible by an application. You can use it to save some setting value or default values or any other form of key value pairs.
So a key,value pair can be saved in one activity can be accessed in other activity.
Different modes means that whether the data saved can be used by other application or not. You should not worry about all modes. As MODE_WORLD_READABLE & MODE_WORLD_WRITEABLE are deprecated now.
For the difference:-
Difference between getShared/get preference
I am having an abstract class having all static objects and function to store some globel data of the application during the execution session.
All data reset to null once I relaunch the application in ICS and above version with Systems's setting ALWAYS_FINISH_ACTIVITIES is set as true.
Whats the better way to mainitan data on application relaunch?
Regards,
Android IT
Editing my question:
I know that Sharepreference can be a better option but I dont want to store data for multiple sessions of the application and data I am storing is huge.
Regards,
Android
IT
Each Android app is running in a dependent process. When an app become background and system need memory, the app's process may be killed by system. In this situation, when app relaunch, static objects will lost.
I think use SharedPreference to maintain data is a better way.
You can store data using SharedPreferences
http://developer.android.com/reference/android/content/SharedPreferences.html
Store the data on the local storage would seem appropriate: http://developer.android.com/training/basics/data-storage/index.html
You can store the data local using a XML file. You can read the XML File on startup with a DOM Reader
http://www.ibm.com/developerworks/library/x-android/
If you have lots of data you can use a SQLite Database
http://developer.android.com/training/basics/data-storage/databases.html
If there are not too much data you can keep them in SharedPreferences
You access them with PreferenceManager.getDefaultSharedPreferences()
Once application exit or destroy that will remove all data . if you want to access after relaunch you should save your data as per your requirement . like local or web. its deepen you. right now you can store data in preference so it will useful after relaunch the application.
I think this is better for you : How to use SharedPreferences in Android to store, fetch and edit values
I have an appliaction which will show a welcome window only the first time the user starts the app. What would be the best way to store this boolean variable (i.e. "isFirstTime") to phone storage? Should I use Shared Preferences or Internal Storage?
The docs say that if I use Internal Storage my "preference" file will automatically get removed upon uninstallation which is quite handy.
I want a clean, simple and fast solution.
Yes, I'd recommend using Shared Preferences. Basically you could put a shared preference with a key of "isFirstTime" and a type of boolean set to false. Then in your main activity do something like:
getBoolean (isFirstTime, true);
This, if it can't find isFirstTime will give you true, allowing you to do an if-statement based on the result.
I agree that SharedPreferences would probably be the most "clean, simple, and fast solution" that you are looking for. SharedPreferences are also deleted when the application is uninstalled.
Are the shared preferences associated with the App deleted when the app is removed?
SharedPreferences are your best option for this.