Responding to preference updates in Android - android

I am calling a PreferenceActivity from another activity and then updating the application state (ie: changing the font size) on onActivityResult, based on the preference changes.
I was thinking it would be better to put the state update logic in the PreferenceActivity. That way I don't have the duplicate the logic in each activity that calls the PreferenceActivity.
What's the best or correct way to do this?

Have any Activity (or other component) that cares about preference changes register a preference change listener via registerOnSharedPreferenceChangeListener(). Then, when the preferences change by any means, they will find out about it and can react accordingly.

The PreferenceActivity should handle all the preference setting. Your other activities should read what those settings are when they run and adjust themselves accordingly.

Related

When to apply design-changes from PreferenceActivity in MainActivity? #onResume?

i`m new to android-coding and am working on my first app. I have the following question:
If one wants the user to be able (for instance) to change the backgroundcolor of the app in PreferenceActivity, where should this change be applied in the MainActivity of the app?
In my case i have many views on a few pages which i want to be customizable in one or the other way. I do it like this:
If there are relevant changes in PreferenceActivity i set a flag and save the changes and the flag in SharedPreferences
Since onResume of MainActivity is called whenever the user returns, i read from the flag in SharedPreferences if changes of the views need to be made and if so, i apply the changes.
I want to use a flag because i dont want to apply the changes again and again whenever onResume is called because its many views that are affected and i dont want to slow down the app unnecessarily.
How would you all do this? i`ll be happy about any hint. Maybe i should even apply the changes in PReferenceActivity already? i dont know......
thx

Using the Android preference framework without PreferenceActivity

I have a preferences.xml, containing various settings for my app - their possible values\names, defaults, etc.
It was previously used simply in a PreferenceActivity with addPreferencesFromResource().
Now I am creating a completely customized settings UI, and I want it to have nothing to do with PreferenceActivity.
All I ever want is to have my preference hierarchy from the XML (as PreferenceScreen), so I can use it to construct my own UI.
I don't want Android's list adapter, I don't want the dialogs, etc. Only the data model.
Sadly, I haven't found a nice way to do that.
The only close thing I can think of is extending PreferenceActivity, providing a custom "R.id.list", and setting its visibility to GONE, so no one will see Android's preference UI.
Any good, clean way to accomplish that?
You can accomplish this by using the PreferenceManager directly. First you would call the setDefaultValues which does the loading of your preferences XML resource. Next you can get access to the SharedPreferences by calling getDefaultSharedPreferences and perform any preference changes through the SharedPreferences object. To edit your preferences you will need to get the Editor from your SharedPreferences object. Make sure to call commit on the editor to actually save the changes you have made.

Per instance widget preferences VS app preferences

My app is currently using a subclass of PreferencesActivity for its preferences.
I am in the process of adding widgets, where each instance has its own preferences (I also offer multiple widget sizes, which means multiple subclasses of AppWidgetProvider).
The widgets and the app share most of their preferences, the widget actually supporting a few items less.
In the process of writing the widget configurator activity, I am realizing that I need to access a preferences layout which does not exist.
As I understand it, is not possible to access the PreferencesActivity layout in the configurator activity. Does this means that I must create a sepecific preferences layout for the configurator activity ? Do you have any tips on optimizing the overlap between app and widget preferences ?
Your configuration activity does not to have to be a PreferenceActivity. Make it a normal Activity and use the SharedPreferences.Editor class to change the preferences the widget uses.

Override Orientation Change But NOT Restart The Activity AND Pass State Data

I want to be able to change the layout when a device is re-orientated to landscape or portrait. For speed and resource purposes (plus other issues applicable to my app) I do NOT want my app to be destroyed and restarted. I have several objects which I wish to retain between orientation changes as there is no benefit from destroying and re-creating them! I simply just want to change the position of some buttons and TextViews so that they suit the current orientation. Easy right?
Well no it isn't. To achieve the above I included in the app Manifest the configChange option for orientation change. Then I've implemented the onConfigurationChanged() where I determine and apply the appropriate layout. Simple yes?
But now take the textview I have in my layout. How on earth, using this particular method of responding to orientation changes, do I put the same text in the previous textview to the new textview? No instance data is passed to onConfigurationChanged() method. Also for some of the buttons, they could be disabled or enabled... I need to know this after orienatation change.
If I let Android destroy and restart my activity it's first going to create unnecessary work. All I want is to just move a few buttons and textviews.. NOT restart the whole app. That's just ludicrous!
Can anyone help me achieve what need?
An easy way to maintain configuration-independent data is to make use of onRetainNonConfigurationInstance() and its companion method getLastNonConfigurationInstance(). Just return an object that contains all the data that you want to reuse when your activity is recreated.
In Honeycomb, or if you are using the Android compatibility package, you can just call Fragment.setRetainInstance(true) instead. See the docs.

Android custom component doesn't retain value on orientation change

I have a layout that uses TextView and a custom control I created that combine a TextView and two buttons for an integer up/down control. The TextView retains its value when the device is rotated, I'm calling the save/restore instance state. I'm not capturing any values as the device retains the values. However, this is not the case for my custom component.
Is there something I need to implement in a custom control so when the device orientation changes it retains the values like the native controls do?
Thank you.
Throw android:configChanges="orientation" into your activity tags in your AndroidManifest.xml file.
You can prevent your activity from being destroyed and recreated as Haphazard suggests, and this is probably the best solution.
If you don't want to do that, then you need override the following three methods:
isSaveEnabled() (override to return true)
onSaveInstanceState()
onRestoreInstanceState(Parcelable)
Consult the documentation (such as it is) for how to do this. There's also an example of how it's done here. The view will need a non-default ID to have it's onSaveInstanceState() called or else you'll also need to override onSaveInstanceState(Bundle) in your activity.

Categories

Resources