What is use of preference activity in android? - android

I have studied preference activity but I don't understand it. Can anybody tell me what is the use of preference activity? How does it differ from activity?
Thanks

PreferencesActivity is a way to easily create preference screens such as those in Android itself, just look under Settings. These can be used inside applications to easily save preferences to SharedPreferences and then easily access these from within your app.
See this page for more information on PreferenceActivity.
An example of a PreferenceActivity showing some preferences:

Use:
It gives a very easy way to create a standard looking settings screen for any application.
Why:
minimal coding is required
How it works:
it takes care of UI.The values are stored in SharedPreferences automatically by the PreferenceActivity and so its scope becomes app wide.
Sample Screen UI:

such settings screen, you can use it for your app where user want to change setting.

Its one stop to manage your Shared Preferences. Through this, you can provide a group of Settings to user for your app. Since it only deals with preferences, that's why its different from regular Activity. Read this for more info

PreferenceActivity supports the simple handling of preferences. It can show a set of preferences to the user.
An activity on the other hand is a single thing a user can do.
Checkout Section 13 at this link

It is usually used to create a settings screen for applications .The values are stored in SharedPreferences automatically as soon as user selects a preference setting. It frees the programmer from explicitly saving preference values.

PreferenceActivity is a class that allows you to implement an activity that realizes a hierarchical organization of choices. Most often it provides a good bases for building the settings activity in you application.

Related

Does Shared preferences get shared across user

I am having some question, kindly help me out, following are my questions?
Does shared preferences data get shared across users?
Also can we have same widget IDs in two different users?
I am having a widget which any user can use. I am getting some
conflicts when a guest user or a any new user is switched to. Specifically, my widget is resizeable and in order to save its state so that it can be inflated accordingly on phone restart or some refresh events. These are working perfectly for the Admin User but it behaves abnormally when a guest is added or if the user is switched.
Yes shared preferences data get shared across users if we define CONTEXT MODE while creating SahredPreference for more detail you can read here for detail, and here. For widget id i think we can give same id in different users but here (i am not sure) also should be concept of Mode.

How to use session management (using shared preference) in more then one android activity (different activity for different account)

I want to create session management in android for more then one activity.
i read session management on this site http://www.androidhive.info/2012/08/android-session-management-using-shared-preferences/
how to create above code for more then one Activity? From above tutorial i can understand for one activity only.
You can use the same logic explained in that link. While using Activities, save the preference data(logged in or not boolean etc) when user leaves or enter the activity class. Like in onPause method.

How to add an information page?

I'm new in Android programming and I need some help.
I'm doing an app for my thesis and I need at the first access(installation) to have an information page(like Google Calendar when recently installed). And the user will be able to go on pressing on a "Next" button.
This "page" must be visible only if the application is removed and after a while reinstalled.
Can someone help me to handle this?
There are many ways to go about this. One of them would be to take advantage of the SharedPreferences in Android. Make a default value of false. When your app starts, check the value in the shared preference that you set. If it is false, show the information dialog and then set it to true. If the value is true, continue with application startup. SharedPreferences will be deleted when you uninstall the application, so your information dialog will be shown again if the user uninstalls and then re-installs your app.
Here are some examples of using SharedPreferences.
You need to store some kind of flag for this, for example you could use SharedPreferences and store a key called "firstLaunch" with a default value of true. At startup read the flag, show info if it's true, and set to false.
There is a library called Showcase View to visualize the most important functions of your app.
The ShowcaseView (SCV) library is designed to highlight and showcase specific parts of apps to the user with a distinctive and attractive overlay. This library is great for pointing out points of interest for users, gestures, or obscure but useful items.
You could use that to develop your information page.

SharedPreferences for general app preferences and user session identificators

I'm using SharedPreferences to save two kinds of preference information.
1) General app preferences like results per page, sessionTime, etc. which are accessible only once you're logged in.
2) A session identifier like the users name and surname. One can see the name on every activity/page so as to know the the session is ok.
Is it ok for me to use sharedPreferences with 2 separate string identifiers. One for general settings and another for session info and manage the usage of these setting through my global application class or is my approach totally wrong?
By design Android does not differ between application/user preferences. Don't bother creating 2 different preference files, just use PreferenceManager.getDefaultSharedPreferences(yourContext) everywhere

SharedPreferences on android

I have a problem with SharedPreferences and my PreferenceActivity. I need that my application can difference between differentes preferences and users, so every user has his own preferences. I thought in use SharedPreferences sending to it the user ID to get the correct options. The problem is that I dont know how to do for my PreferenceActivity use that specific options. Currently, It is using the context options, so when I restart my application, it loads the last options modifieds...
How can I configure my preferenceActivities to store his changes in the sharedpreferences that I want? And for the load?
Thanks!
It's unusual to support the concept of 'users' on Android - most apps assume a phone has a single user.
That said, you can create custom SharedPreferences like this
SharedPreferences userprefs = getSharedPreferences(username,MODE_WORLD_READABLE);
For your PreferenceActivity, in onCreate you do this
getPreferenceManager().setSharedPreferencesName(username);
Hope that helps...
This might be a bit late, but you can also check out Swarm's Cloud Data, which provides a per-user SharedPreferences system, stored in the cloud (so if the user switches devices, their preferences persist on the new device).

Categories

Resources