How could I use the same set of preference screens for all Android versions from 2.X to 4.X?

后端 未结 3 1971
我在风中等你
我在风中等你 2020-12-14 21:21

NOTICE: Please save yourself some time and refer to the accepted answer, no need to read all the quesiton.
You may read the rest of the question and the

3条回答
  •  半阙折子戏
    2020-12-14 21:48

    You can use this class to display a preference screen in all Android versions from 2.X to 4.X, by feeding it with a preference screen resource.

    You may use it directly by renaming it if you like, but I'd suggest you to add it to your project as is, and inherit from it, which is much cleaner if you need to work with several parent preference screens.

    If you'd like to use it directly, just replace prefs value with your preference screen resource ID.

    If you'd like to inherit from it, you should do it like this:

    import android.os.Bundle;
    
    public class MyPreferencesActivity extends CompatiblePreferenceActivity
    {   
        @Override
        protected void onCreate(final Bundle savedInstanceState)
        {
            setPrefs(R.xml.mypreferencesactivity);
            super.onCreate(savedInstanceState);
        }   
    }
    

    ALWAYS call setPrefs(int) before calling super.onCreate(Bundle)

    If, for some reason, you'd just like to take advantage of the glitch-fix and create preferences on your own, you may either just copy the glitch-fix code into your own preference activity, or inherit from the class and catch the PrefsNotSet exception as follows:

    import android.os.Bundle;
    
    public class MyPreferencesActivity extends CompatiblePreferenceActivity
    {   
        @Override
        protected void onCreate(final Bundle savedInstanceState)
        {
            try{
                super.onCreate(savedInstanceState);
            }catch(PrefsNotSetException e){};
        }   
    }
    

    And finally, the class:

    import android.annotation.TargetApi;
    import android.os.Bundle;
    import android.preference.Preference;
    import android.preference.PreferenceActivity;
    import android.preference.PreferenceFragment;
    import android.preference.PreferenceScreen;
    
    public class CompatiblePreferenceActivity extends PreferenceActivity
    {
        private int prefs=0;
    
        //Get/Set
        public void setPrefs(int prefs)
        {
            this.prefs=prefs;
        }
    
        //Exception
        protected static class PrefsNotSetException extends RuntimeException
        {
            private static final long serialVersionUID = 1L;
            PrefsNotSetException()
            {
                super("\"prefs\" should be set to a valid preference resource ID.");
            }
        }
    
        //Creation
        @Override
        protected void onCreate(final Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            if (prefs==0)
                throw new PrefsNotSetException();
            else
                try {
                    getClass().getMethod("getFragmentManager");
                    AddResourceApi11AndGreater();
                    }
                catch (NoSuchMethodException e) { //Api < 11
                        AddResourceApiLessThan11();
                    }
        }
    
        @SuppressWarnings("deprecation")
        protected void AddResourceApiLessThan11()
        {
            addPreferencesFromResource(prefs);
        }
    
        @TargetApi(11)
        protected void AddResourceApi11AndGreater()
        {
            PF.prefs=prefs;
            getFragmentManager().beginTransaction().replace(
                android.R.id.content, new PF()).commit();
        }
    
        @TargetApi(11)
        public static class PF extends PreferenceFragment
        {
            private static int prefs;
            @Override
            public void onCreate(final Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(prefs);
            }
        }
    
        //Sub-screen background glitch fix
        @SuppressWarnings("deprecation")
        @Override
        public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen,
            Preference preference)
        {
            super.onPreferenceTreeClick(preferenceScreen, preference);
            if (preference!=null)
                if (preference instanceof PreferenceScreen)
                    if (((PreferenceScreen)preference).getDialog()!=null)
                        ((PreferenceScreen)preference).getDialog().
                            getWindow().getDecorView().
                            setBackgroundDrawable(this.getWindow().
                                getDecorView().getBackground().getConstantState().
                                newDrawable());
            return false;
        }
    }
    

提交回复
热议问题