Skip to content

Commit

Permalink
Add shared preferences support for dialog fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
pranavpandey committed May 24, 2022
1 parent 2103297 commit 2bd4443
Showing 1 changed file with 34 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.KeyEvent;
import android.view.View;

Expand All @@ -42,7 +44,8 @@
* @see #onCustomiseBuilder(DynamicDialog.Builder, Bundle)
* @see #onCustomiseDialog(DynamicDialog, View, Bundle)
*/
public class DynamicDialogFragment extends AppCompatDialogFragment {
public class DynamicDialogFragment extends AppCompatDialogFragment
implements SharedPreferences.OnSharedPreferenceChangeListener {

/**
* Default button color. it will be used internally if there is no button color is applied.
Expand Down Expand Up @@ -82,7 +85,7 @@ public class DynamicDialogFragment extends AppCompatDialogFragment {
private boolean mAutoDismiss = false;

/**
* Dialog builder to customise this fragment according to the need.
* Dialog builder to customise this fragment according to the requirements.
*/
private DynamicDialog.Builder mDynamicDialogBuilder;

Expand Down Expand Up @@ -228,8 +231,33 @@ protected void onCustomiseDialog(@NonNull DynamicDialog alertDialog,
return alertDialog;
}

/**
* Returns whether to register a shared preferences listener for this fragment.
*
* @return {@code true} to register a {@link SharedPreferences.OnSharedPreferenceChangeListener}
* to receive preference change callback.
*/
public boolean isOnSharedPreferenceChangeListener() {
return false;
}

@Override
public void onResume() {
super.onResume();

if (isOnSharedPreferenceChangeListener() && getContext() != null) {
PreferenceManager.getDefaultSharedPreferences(requireContext())
.registerOnSharedPreferenceChangeListener(this);
}
}

@Override
public void onPause() {
if (isOnSharedPreferenceChangeListener() && getContext() != null) {
PreferenceManager.getDefaultSharedPreferences(requireContext())
.unregisterOnSharedPreferenceChangeListener(this);
}

if (mAutoDismiss) {
dismiss();
}
Expand Down Expand Up @@ -406,7 +434,7 @@ protected boolean isAutoDismiss() {
/**
* The dynamic dialog builder set for this fragment.
*
* @return The dialog builder to customise this fragment according to the need.
* @return The dialog builder to customise this fragment according to the requirements.
*/
protected @Nullable DynamicDialog.Builder getBuilder() {
return mDynamicDialogBuilder;
Expand Down Expand Up @@ -584,4 +612,7 @@ protected void finishActivity() {
}
}
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { }
}

0 comments on commit 2bd4443

Please sign in to comment.