0

I have the following class in my android code:

public class UserSettingActivity extends PreferenceActivity{

    private Preference myPreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
        myPreference = findPreference("reset");
        myPreference.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
            public boolean onPreferenceClick(Preference arg0) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(UserSettingActivity.this);
                alertDialog.setMessage("Are you sure to delete the database?");
                alertDialog.setCancelable(true);
                alertDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        final DBAdapter db = new DBAdapter(getApplicationContext());
                        db.open();
                        db.resetDatabase();

                    } });
                alertDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.cancel();
                    } });
                alertDialog.show();
                return false;
            }
        });

    }

}

with the associated xml file as follows:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <EditTextPreference android:title="Your Name"
        android:key="username"
        android:summary="Please provide your username"></EditTextPreference>
    <CheckBoxPreference android:title="Application Updates"
        android:defaultValue="false"
        android:summary="This option if selected will allow the application to check for latest versions."
        android:key="applicationUpdates" />

    <Preference
        android:key="reset"
        android:title="Reset database"
        android:summary="This will remove every entry in the database"
         />
</PreferenceScreen>

in which the methods addPreferencesFromResource and findPreference are marked as deprecated. I found a hint to follow the example given here. Does this mean, I have to completely rewrite my java class and the xml layout file as well? Or can I reuse the xml layout file? Or do I neeed to create another xml layout file? I see two of them in the example...

Also, I do not see any text in the xml layoutfiles in the example referencing the text fragmented_preferences. I am utterly confused! I cannot possible begin to translate my 'old' code to the 'new' code!

How to fix this problem, i.e. to replace the deprecated methods in my code?

Alex
  • 41,580
  • 88
  • 260
  • 469
  • `PreferenceActivity` is deprecated, meaning "we have something else that we would prefer that you use". That "something else" is `PreferenceFragment`, which can be hosted by any activity, not just a `PreferenceActivity`. That being said, nobody is pointing a gun at your head to use `PreferenceFragment` instead of `PreferenceActivity`. It is possible, though somewhat unlikely, that `PreferenceActivity` might be removed outright, so *sometime* you might want to move to `PreferenceFragment`. – CommonsWare Dec 06 '15 at 16:44
  • Would be good to know what a `PreferenceActivity` and a `PreferenceFragment` really is. I mean, explained in a way that makes sense... – Alex Dec 06 '15 at 16:45
  • In short: it's the guts behind the UI presentation of the Settings app. Originally, it was activity-focused, because fragments did not exist. In API Level 11, they migrated to fragments, to better handle tablet-sized screens. FWIW, there's a couple of chapters on preferences in [my book](https://commonsware.com/Android). – CommonsWare Dec 06 '15 at 16:54

0 Answers0