Android’s PreferenceActivity for all API versions

      網友投稿 756 2025-03-31

      官方文檔

      I have spent the last few days learning about how to use the new Android PreferenceFragment which requires PreferenceActivity to override a new v11 (Honeycomb) method called onBuildHeaders(). Unfortunately, the documentation is not very clear how one would create a single PreferenceActivity that could play well in all versions, utilizing the newest features if you have it and avoiding an app crash on older Android versions. I encountered several solutions to this issue by creating two different activities for the two different mechanisms requiring two entries in your AndroidManifest.xml file. Having two different PreferenceActivities means if you have library code that extends that class, you now have to duplicate it. Then, if your app descends your library class, now has to be duplicated yet again. The end result is … less than ideal.

      Thankfully, after spending a great deal of time on the subject, I have come up with a single class solution that will work in all Android versions. A couple of the newer methods need to be found using reflection and based off of them, in addition to using the newly introduced xml file of , allow for a single class solution.

      First, take your old xml layouts and break them up into several files based on how you would like to see them categorized using the new header format. Simply breaking them apart so that each PreferenceCategory has its own file is a good start. For our example here, we shall assume there are 3 such files, all in the res/xml folder: app_prefs_cat1.xml, app_prefs_cat2.xml, and app_prefs_cat3.xml

      Now let us create the descendant PrefsActivity class:

      import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.List; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; public class PrefsActivity extends PreferenceActivity { protected Method mLoadHeaders = null; protected Method mHasHeaders = null; /** * Checks to see if using new v11+ way of handling PrefsFragments. * @return Returns false pre-v11, else checks to see if using headers. */ public boolean isNewV11Prefs() { if (mHasHeaders!=null && mLoadHeaders!=null) { try { return (Boolean)mHasHeaders.invoke(this); } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } } return false; } @Override public void onCreate(Bundle aSavedState) { //onBuildHeaders() will be called during super.onCreate() try { mLoadHeaders = getClass().getMethod("loadHeadersFromResource", int.class, List.class ); mHasHeaders = getClass().getMethod("hasHeaders"); } catch (NoSuchMethodException e) { } super.onCreate(aSavedState); if (!isNewV11Prefs()) { addPreferencesFromResource(R.xml.app_prefs_cat1); addPreferencesFromResource(R.xml.app_prefs_cat2); addPreferencesFromResource(R.xml.app_prefs_cat3); } } @Override public void onBuildHeaders(List

      aTarget) { try { mLoadHeaders.invoke(this,new Object[]{R.xml.pref_headers,aTarget}); } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } } static public class PrefsFragment extends PreferenceFragment { @Override public void onCreate(Bundle aSavedState) { super.onCreate(aSavedState); Context anAct = getActivity().getApplicationContext(); int thePrefRes = anAct.getResources().getIdentifier(getArguments().getString("pref-resource"), "xml",anAct.getPackageName()); addPreferencesFromResource(thePrefRes); } } }

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      32

      33

      34

      35

      36

      37

      38

      39

      40

      41

      42

      43

      44

      45

      46

      47

      48

      49

      50

      51

      52

      53

      54

      55

      56

      57

      58

      59

      60

      61

      62

      63

      64

      65

      66

      67

      68

      On older Android versions, the reflected methods will be null and thus avoids calling the newer methods, using the older mechanism of adding the various preference xml files in onCreate(). In order to use the new Honeycomb (v11+) preference mechanism, we need to create one more xml file – the newer xml file that ties all the category files together. The res/xml resource file “pref_headers.xml”:

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      12

      13

      14

      15

      16

      17

      18

      Android’s PreferenceActivity for all API versions

      19

      20

      21

      22

      23

      24

      25

      26

      27

      28

      29

      30

      31

      The first three headers list the three standard category xml files we created, but you are also free to add more items as you see fit, just remember that those will only be visible on devices with Android 3.0 or later.

      Now that we have our xml file and single preference activity, we can define it in our AndroidManifest.xml as usual. Note: do not use android:launchMode=”singleTop” for your prefs activity or else it will break phones using 4.0+ by never showing more than just the header list.

      1

      2

      3

      4

      5

      6

      7

      We can have a menu call our prefs activity with just one line:

      startActivity(new Intent(this, PrefsActivity.class);

      1

      Now that you have a descendant class like this one, you can freely use it in all versions of Android and it will automatically use the newer mechanism where appropriate.

      查看這里

      Android API

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:Excel進行函數公式復制操作的具體方法
      下一篇:協同互聯平臺建設項目(協同創新平臺建設方案)
      相關文章
      国产偷国产偷亚洲高清人| 亚洲精品123区在线观看| 亚洲av无码一区二区三区在线播放 | 亚洲精品无码专区在线| 亚洲国产av一区二区三区丶| 337p日本欧洲亚洲大胆精品555588 | 久久久久久久久亚洲| 国产成人A人亚洲精品无码| 亚洲精品国产品国语在线| 伊人久久大香线蕉亚洲五月天| 久久精品国产亚洲5555| 亚洲熟女一区二区三区| 亚洲成AV人在线播放无码| 亚洲avav天堂av在线不卡| 久久亚洲精品成人777大小说| 久久亚洲精品中文字幕无码| 亚洲午夜免费视频| 久久精品国产亚洲77777| 亚洲视频在线免费播放| 亚洲成年人免费网站| 亚洲不卡在线观看| 成人亚洲国产va天堂| 亚洲heyzo专区无码综合| 日韩亚洲人成网站| 国产亚洲成人久久| 久久精品亚洲中文字幕无码网站| 亚洲AV福利天堂一区二区三| 亚洲综合久久综合激情久久| 亚洲精品国产免费| 亚洲人成小说网站色| 亚洲AV无码资源在线观看| 国产成人亚洲精品蜜芽影院| 亚洲精品国产综合久久一线| 国产亚洲精AA在线观看SEE| 亚洲国产精品热久久| 亚洲丰满熟女一区二区v| 亚洲色偷偷色噜噜狠狠99| 日韩色视频一区二区三区亚洲| 亚洲精品一级无码中文字幕| 亚洲免费人成在线视频观看| 337p欧洲亚洲大胆艺术|