0

I'm using AppCompatPreferenceActivity, that is an Activity which extends PreferenceActivity and has an AppCompatDelegate. I want to add a headless fragment to this Activity, but I can't call to getSupportFragmentManager...

Is there a way to add fragments to a PreferenceActivity using AppCompatDelegate?

Sergio Viudes
  • 2,714
  • 5
  • 26
  • 44

1 Answers1

3

The only way to call getSupportFragmentManager is from a FragmentActivity or something which derives from it. PreferenceActivity derives from Activity which cannot use Fragments.

You should look into using a PreferenceFragment instead.

vguzzi
  • 2,420
  • 2
  • 15
  • 19
  • PreferenceFragment is only compatible with API >= 11. I tried using Preference Support Library to use PreferenceFragmentCompat, but it I cannot use my custom preferences with it... (my custom preferences are overriding methods that are not available in the new preferences in Preference Support Library, so I don't know how to "migrate" my custom preferences for using them with Preference Support Library) – Sergio Viudes Oct 13 '15 at 10:05
  • Could you paste the code you are trying to use which aren't supported in PreferenceFragmentCompat. – vguzzi Oct 13 '15 at 10:20
  • I mean that creating custom preferences with Preference Support Library is not documented yet, and it seems it's a bit "tricky", as you can see here: http://stackoverflow.com/questions/32621403/how-do-i-create-custom-preferences-using-android-support-v7-preference-library I have several custom preferences in my project. So I think is not a good idea to upgrade to Preference Support Library yet. – Sergio Viudes Oct 13 '15 at 10:21
  • Is there a reason you need to add a headless Fragment over using something else? Have you thought about creating a static utility class instead of just including the code in your Activity? The more information you give the easier a solution will be found. When supporting much older API's you sometimes can't keep to the best practices as libraries have been deprecated and can't be used in that fashion. – vguzzi Oct 13 '15 at 10:27
  • I would want to add a headless fragment to execute an AsyncTask and handle configuration changes. Something like this: http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html – Sergio Viudes Oct 13 '15 at 10:32
  • You may be better off looking at saving all of the required information in onSaveInstanceState (an Activity lifecycle event which gets called before the activity is destroyed) and getting it back again in onRestoreInstanceState. While retaining the information in a worker fragment will save a slight amount of processing time if you cannot use it for your own reasons then I think this would be your best option. – vguzzi Oct 13 '15 at 10:42
  • Heres a link to one of my other answers demonstrating how to save a videoView position with onSaveInstanceState and onRestoreInstanceState - http://stackoverflow.com/questions/33073394/media-player-not-load-when-activity-recreated-on-orientation-change-android/33087788#33087788 – vguzzi Oct 13 '15 at 10:43
  • Just override it in the Activity you're using and save all of the information you are loosing in there, and retrieve it again afterwards. – vguzzi Oct 13 '15 at 10:43