NLemay
NLemay

Reputation: 2383

Android app : show PreferenceFragment as a dialog?

I'm new to Android (with iOS background) and I would like to do the following : I've made a PreferenceFragment in which I ask the user his credentials to connect to my WebServices. When I detect that those credentials are rejected by my server, I would like to show to my user a dialog (modal) where he can edit his preferences (credentials).

I've manage to do it using PreferenceActivity. So when I click a bouton, I execute the following code :

Intent settingsActivity = new Intent(getActivity().getBaseContext(), PreferencesConnection.class);
startActivity(settingsActivity);

Which load a PreferenceActivity as a Dialog with an XML ressource :

addPreferencesFromResource(R.xml.preferences_connection);

But this is depreciated. I can I do the same, but with Fragment? Should I use DialogFragment to show my PreferenceFragment, or not? Would it be easier to just rebuilt my credentials in a DialogFragment? Should I use a Activity instead of Fragment?

Upvotes: 4

Views: 5261

Answers (1)

Howard Hodson
Howard Hodson

Reputation: 943

To do an old fashioned, single page settings screen without PreferenceHeaders, do as follows:

In your activity that invokes the settings screen (example is in onMenuItemSelected):

case R.id.menuSettings:
        Intent settingsIntent = new Intent(this, SettingsActivity.class);
        startActivityForResult(settingsIntent, GC.SETTINGS_ACTIVITY_ID);
        break;

In your XML folder, create an old fashioned PreferenceScreen. PreferenceCategory, ListPreference, CheckBoxPreference, etc. Do not use PreferenceHeaders

Create a settings activity:

package com.mycompany.project1;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;

public class SettingsActivity extends PreferenceActivity {

    private final static String TAG = "SettingsAcitivity";

    public SettingsActivity() {}

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyLog.d(TAG, "onCreate");
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new LocationFragment()).commit();
    }

    public static class LocationFragment extends PreferenceFragment {

        private String TAG = "LocationFragment";

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            MyLog.d(TAG, "onCreate");
            addPreferencesFromResource(R.xml.settings);
        }
    }
}

Do not associate a dialog theme with the settings activity. You will get weird results for certain preference types such as radio buttons. (I tried the dialog thing and couldn't find the coding error(s). Everything worked fine when I used the standard activity theme instead of the dialog theme. Evidently the holo dialog theme collides with settings definitions.

Note: I used startActivityforResult so I could reinitialize specific settings values that changed.

Upvotes: 1

Related Questions