0

From the book, Hello Android 3rd Edition. I was doing this example and I found out that one of the examples that he took, uses deprecated method.

Here it is.

public class Prefs extends PreferenceActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.settings);
  }
}

The addPreferencesFromResource is deprecated. Now here are my few questions, and I'd very much appreciate pointwise answers to each if possible.

  • My minimum sdk version is 10. I google something and found out that I could do it using

    getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new MyPreferenceFragment())
                .commit();
    
    public static class MyPreferenceFragment extends PreferenceFragment {
    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
    }
    }
    

    Now this will work for all the devices with min sdk > 11, or will it work for all the devices?

  • Now, in my manifest, my minSDK is 10 and the above code says that min required should be 11. So when I change it to 11, and try to run it on my Samsung galaxy ace, it does not find my phone, but when I change it back to 10, my phone becomes discoverable.

  • Finally I put@SuppressLint("NewApi") on the top of my Prefs class definition, and within on create, I check if Build.version.SDK_int is > 11 then do the above code, else do the previous deprecated code. Is this the right approach?

Thanks

Kraken
  • 23,393
  • 37
  • 102
  • 162
  • This [answer](http://stackoverflow.com/a/16374538/2556111), thanks to CommonsWare, my help. – ramaral Jan 04 '14 at 15:14
  • I also check for Build version. If < 11, I use the PrefereceActivity, (yes, I had to suppress the poor Lint!) else the PreferenceFragment. – Phantômaxx Jan 04 '14 at 17:15

0 Answers0