Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to addPreferencesFromResource as its deprecated

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.

like image 669
Android Stack Avatar asked May 04 '13 12:05

Android Stack


1 Answers

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

like image 175
CommonsWare Avatar answered Sep 27 '22 22:09

CommonsWare