How do I access PreferenceFragment by id?

I have a cell style style. I define the headers:

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android"> <header android:id="@+id/pref_general" android:fragment="MyPreferencesFragment" android:title="@string/pref_general_title"> <extra android:name="resource" android:value="pref_general" /> </header> <header android:id="@+id/pref_sharing" android:fragment="MyPreferencesFragment" android:title="@string/pref_sharing_title"> <extra android:name="resource" android:value="pref_sharing" /> </header> </preference-headers> 

Then I load them into PreferenceActivity:

 public void onBuildHeaders(List<Header> target) { loadHeadersFromResource(R.xml.preference_headers, target); } 

How can I then specify the exact fragment by its identifier in startPreferenceFragment? How can I access the list item corresponding to this fragment so that I can enable / disable it?

+4
source share
2 answers

I have found a solution. It is not possible to access the fragment directly, but it can be found through the ListAdapter . In PreferenceActivity you can write:

 int fragmentId = R.id.pref_sharing; for (int i = 0; i < getListAdapter().getCount(); i++) { Header header = (Header) getListAdapter().getItem(i); if (fragmentId == header.id) { // You can access a fragment name (class) String fragment = header.fragment; // You can access fragment arguments Bundle args = header.fragmentArguments; } } 

I did not find a way to disable the header list item, but I created a workaround based on this code.

+2
source

You can directly access PreferenceFragment. Actually, there are probably several ways to do this. I tried the first one below and it definitely works. The other two I have not tried myself, but I see no reason why they should not work.

  • Override onAttach() in your fragment and pass a reference to yourself (i.e. this ) on the activity passed. Save a copy of the link in the host action and use it to call any method that you want to use your fragment immediately later. See here for an example. Credit is sent to @Devunwired for this solution.
  • Same as (1), but pass the id of the host activity fragment instead of this . You can use getId to get the fragment identifier in the onAttach() method. Then use regular findFragmentById in your activity to extract the fragment whenever you want.
  • Update the static int variable in your host activity (directly or through the public method) with the fragment identifier, which you can find using getId in onAttach() or onCreate() your fragment. Then use findFragmentById in your activity as before. This implies less coding than (1) or (2), but it's an ugly hack!

It would be nice if Android made it easier to access snippets in the headers directly. For some time I could not understand why using the android:id value in the XML layout did not work with findFragmentById until I understood header is not a fragment!

0
source

Source: https://habr.com/ru/post/1384207/


All Articles