Android PreferenceActivity and dialog snippets

The application I'm developing has an activity that extends SherlockFragmentActivity . I would like to use api settings to easily add preferences to activity. Since I would like to support api level 8 and above, I have to extend the activity from the SherlockPreferenceActivity class.

The problem is that to work you need to show a dialogue. The dialog extends SherlockDialogFragment . The show() method for a dialog requires two parameters: a FragmentManager object and a String tag.
To get the FragmentManager object, I used the getSupportFragmentManager() method to work. This method is missing from SherlockPreferenceActivity . I tried using getFragmentManager() , but Eclipse says that

The method is shown (FragmentManager, String) in type DialogFragment not applicable for arguments (FragmentManager, String)

How can I show a dialog fragment from SherlockPreferenceActivity ?

+6
source share
2 answers

You should use Shared Preferences instead of using PreferenceActivity . Declare these links in a separate helper class, and do not extend it to Activity. This gives you the flexibility to create a custom layout.

Example:

 public class SharePrefManager { // Shared Preferences SharedPreferences pref; // Editor for Shared preferences Editor editor; // Context Context _context; // Shared pref mode int PRIVATE_MODE = 0; // Sharedpref file name private static final String PREF_NAME = "selfhelppref"; //Your configurable fields public static final String KEY_PREF1 = "pref1"; public static final String KEY_PREF2 = "pref2"; public static final String KEY_PREF3 = "pref3"; public SharePrefManager(Context context){ this._context = context; pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE); editor = pref.edit(); } //Setter function for configurable field public void setPref(String key, String value){ editor.putString(key, value); } //Getter function for configurable field public String getPref(String key){ return editor.getString(key); } } 

Link to your activity

 SharePrefManager SM = new SharePrefManager(this); SM.setPref(SM.KEY_PREF1, "name"); String value = SM.getPref(SM.KEY_PREF1); 
0
source

Try using SherlockDialogFragment.getSherlockActivity().getSupportFragmentManager() .

Example: mySherlockDialogFragment.show(mySherlockDialogFragment.getSherlockActivity().getSupportFragmentManager(), "my_tag");

0
source

All Articles