Alternative to addPreferencesFromResource as its deprecated

39,988

PreferenceActivity is not deprecated.

addPreferencesFromResource() on PreferenceActivity is deprecated. However, if you are supporting API Level 10 or lower, you have no choice but to use it, at least on those devices.

The non-deprecated approach is to use PreferenceFragment in conjunction with PreferenceActivity, as is described in the PrefereceActivity documentation. If your app is only supporting API Level 11 and higher, just use that. If your app is supporting older devices, either:

  • Use addPreferencesFromResource() all the time, as it still works, until you drop support for the older versions, or

  • Use addPreferencesFromResource() only on the older devices (by checking Build.VERSION.SDK_INT), and rely on the new fragment-based system on newer devices

Share:
39,988
Android Stack
Author by

Android Stack

So far did not go beyond the first step in Android development .

Updated on August 03, 2022

Comments

  • Android Stack
    Android Stack almost 2 years

    I create Preference activity on my app to allow user to start/stop background splash screen music as follow :

    public class Prefs extends PreferenceActivity{
    
    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);    
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
    
    
        }    
    }
    

    and inside xml folder create prefs.xml :

     <?xml version="1.0" encoding="utf-8" ?> 
        <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
          <CheckBoxPreference 
             android:title="splash music" 
             android:defaultValue="true" 
             android:key="checkbox" 
             android:summary="Plese remove music "/>
        </PreferenceScreen>
    

    and this code for splash activity :

     public class Splash extends Activity{  
        MediaPlayer ourSong;
    @Override
    protected void onCreate(Bundle Drandroid) {
        this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
        // TODO Auto-generated method stub
        super.onCreate(Drandroid);
             setContentView(R.layout.splash);  
    
        ourSong = MediaPlayer.create(Splash.this, R.raw.splashsound); 
    
         SharedPreferences getPrefs = 
                   PreferenceManager.getDefaultSharedPreferences(getBaseContext());
        boolean music = getPrefs.getBoolean("checkbox", true);
        if (music == true)      
        ourSong.start();
    
        Thread timer = new Thread(){
            public void run(){
                try{
                    sleep(5000); }
                  catch (InterruptedException e){
                    e.printStackTrace(); }
                  finally{
        Intent openTurkeyTrip = new Intent("com.android.dr.MENU");
    
            startActivity(openplanet); }}                                   
                                    };
             timer.start();   }
    
    @Override
    protected void onPause() {
                // TODO Auto-generated method stub
        super.onPause();
        ourSong.release();
        finish();
              } 
           }
    

    how can i solve it with other class which is not deprecated also my app will support old and new devices as below :

      <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    

    any advice will be appreciated, thanks.

  • Android Stack
    Android Stack about 11 years
    my app will support both old and new devices ( post edited) , how to get it work for both as you mention : Use addPreferencesFromResource() only on the older devices (by checking Build.VERSION.SDK_INT), and rely on the new fragment-based system on newer devices in code , please , thanks
  • CommonsWare
    CommonsWare about 11 years
    @AndroidStack: That goes a bit beyond what can reasonably be covered in a StackOverflow answer. Here are some sample projects that demonstrate the results: github.com/commonsguy/cw-omnibus/tree/master/Prefs/Fragments‌​BC github.com/commonsguy/cw-omnibus/tree/master/Prefs/SingleHea‌​der github.com/commonsguy/cw-omnibus/tree/master/Prefs/… That being said, unless you are a fairly experienced Android developer, I would recommend that you just use addPreferencesFromResource() until you drop support for older devices.
  • Android Stack
    Android Stack about 11 years
    would you please my dear sir check this post , thanks .:stackoverflow.com/questions/16379420/…
  • hamza khan
    hamza khan about 4 years
    The link given above has also been deprecated in API 29, for androidX see the documantion of Preference Activity here : developer.android.com/reference/androidx/preference/…