tags:

views:

393

answers:

2

My application is used on multiple platforms so it saves it preferences to a file (rather than to the standard Android SharedPreferences).

Is there any easy of reusing the PreferenceActivity to save preferences to a file or is it a case of creating a whole new activity to do the job? If the latter is the case is there a layout I can use that will make the activity look like the normal preferences screen? PreferenceActivity uses com.android.internal.R.layout.preference_list_content but this doesn't appear to be available to apps for reuse.

+2  A: 

Is there any easy of reusing the PreferenceActivity to save preferences to a file or is it a case of creating a whole new activity to do the job?

Not really. I mean, you could subclass SharedPreferences, rip the guts out, and map it to your own data model, but that would be far, far worse for maintainability than just using SharedPreferences in the first place.

If the latter is the case is there a layout I can use that will make the activity look like the normal preferences screen?

It's just a ListView. It will take you a lot more time to do this than to just use SharedPreferences.

PreferenceActivity uses com.android.internal.R.layout.preference_list_content but this doesn't appear to be available to apps for reuse.

Sure it is. If you have the SDK installed, it's on your hard drive right now. Look in $ANDROID_HOME/platforms/$API/data/res/layout, where $ANDROID_HOME is where you have the SDK and $API is some API (e.g., android-2.1).

CommonsWare
Thanks for the pointer to R.layout.preference_list_content. I missed that directory.I revisited my code from yesterday and as it happens PreferenceActivity works just fine. All I need to do is read and write to my external preferences file in the onCreate() and onStop() methods. For some reason it didn't work for me last night. Must have been the late hour.
Adrian
:: blink :: That works? Cool! I would have expected that to be more painful than it sounds. My apologies for the extra words of warning, then.
CommonsWare
+1  A: 

You can create a function that exports the data from SharedPreferences to a file.

fhucho