Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between DialogPreference before and after AndroidX

We are currently migrating to Androidx namespace with our Android app project. However I noticed that not only the namespace seems to have changed. For DialogPreference also some interfaces which were using before are now missing

  • new interfaces: https://developer.android.com/reference/androidx/preference/DialogPreference
  • old interfaces: https://developer.android.com/reference/kotlin/android/preference/DialogPreference

For example the following methods seem to be missing: onBindDialogView, showDialog, onDialogClosed.

Since we use some of these methods to influence the default behavior of the dialog, it is unclear to me how I should realize this functionality now. For example we are validating the input before closing the dialog, we are saving the value in a database instead of the sharedpreferences and adding some dynamic elements to the dialog.

Has anyone else already encountered this problem and found a solution? Did I miss anything in the documentation? Is there another concept that we can / should use?

It would be possible to use Fragments instead of DialogPreference but for small amounts of content (e.g. a list of tree items, where the user can choose from) this seems to be a lot of overhead for me...

like image 630
little_planet Avatar asked Oct 11 '18 07:10

little_planet


2 Answers

Starting from androidx source files, I've migrated custom classes based on old DialogPreference to new androidx.preference.DialogPreference with the following procedure:

Step 1

The old custom dialog class (e.g. CustomDialogPreference) based on legacy DialogPreference should be split into two separate classes:

  1. One class (e.g. CustomPreference) should extend androidx.preference.DialogPreference and will contain only the code related to preference handling (data management).
  2. Another class (e.g. CustomDialog) should extend androidx.preference.PreferenceDialogFragmentCompat and will contain only the code related to dialog handling (user interface), including onDialogClosed. This class should expose a static method newInstance to return an instance of this class.

Step 2

In the main fragment handling preferences based on PreferenceFragmentCompat the onDisplayPreferenceDialog method should be overridden to show the custom dialog, e.g.:

    private static final String DIALOG_FRAGMENT_TAG = "CustomPreference";      @Override     public void onDisplayPreferenceDialog(Preference preference) {         if (getParentFragmentManager().findFragmentByTag(DIALOG_FRAGMENT_TAG) != null) {             return;         }          if (preference instanceof CustomPreference) {             final DialogFragment f = CustomDialog.newInstance(preference.getKey());             f.setTargetFragment(this, 0);             f.show(getParentFragmentManager(), DIALOG_FRAGMENT_TAG);         } else {             super.onDisplayPreferenceDialog(preference);         }     } 
like image 81
Livio Avatar answered Sep 21 '22 17:09

Livio


Instead of using DialogPreference, you can write your own custom Preference with an AlertDialog. This may be a workaround for those who don't want to deal with the DialogPreference and PreferenceDialogFragmentCompat.

import android.content.Context; import androidx.appcompat.app.AlertDialog; import androidx.preference.Preference;  public class CustomDialogPreference extends Preference {     private final Context context;     public CustomDialogPreference(Context context, AttributeSet attrs) {         super(context, attrs);         this.context = context;     }          @Override     protected void onClick() { //what happens when clicked on the preference         AlertDialog.Builder builder = new AlertDialog.Builder(context);         builder.setTitle("TITLE")                 .setMessage("message")                 .setPositiveButton("OK", (dialog, which) -> {                     String preferenceKey = getKey(); //You can update the SharedPreference with the key                     //....                })                 .setNegativeButton("CANCEL", (dialog, which) -> {                     //....                 })                 .create().show();     } } 

onClick() and getKey() methods belong to the Preference class. The context object comes with the constructor and so on..

The key can be defined, as other preferences, in xml file or programmatically in the PreferenceFragment.

<com.myApp.CustomDialogPreference     android:key="my_preference_key"     android:summary="..."     android:title="..." /> 
like image 30
Ahmed Avatar answered Sep 22 '22 17:09

Ahmed