Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow manually displayPreferenceDialog #106

Merged
merged 3 commits into from
Nov 23, 2017
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,18 +92,12 @@ public void onCreatePreferences(@Nullable Bundle savedInstanceState, String root
@Override
public void onDisplayPreferenceDialog(Preference preference) {
if (this.getFragmentManager().findFragmentByTag(FRAGMENT_DIALOG_TAG) == null) {
Object f = null;

if (preference instanceof EditTextPreference) {
f = EditTextPreferenceDialogFragmentCompat.newInstance(preference.getKey());
displayPreferenceDialog(new EditTextPreferenceDialogFragmentCompat(), preference.getKey());
} else if (dialogPreferences.containsKey(preference.getClass())) {
try {
Fragment fragment = dialogPreferences.get(preference.getClass()).newInstance();
Bundle b = new Bundle(1);
b.putString("key", preference.getKey());
fragment.setArguments(b);

f = fragment;
displayPreferenceDialog(dialogPreferences.get(preference.getClass()).newInstance(),
preference.getKey());
} catch (java.lang.InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
Expand All @@ -112,17 +106,25 @@ public void onDisplayPreferenceDialog(Preference preference) {
} else {
super.onDisplayPreferenceDialog(preference);
}
}
}

if (f != null) {
if (f instanceof DialogFragment) {
((DialogFragment) f).setTargetFragment(this, 0);
((DialogFragment) f).show(this.getFragmentManager(), FRAGMENT_DIALOG_TAG);
} else {
this.getFragmentManager()
.beginTransaction()
.add((Fragment) f, FRAGMENT_DIALOG_TAG)
.commit();
}
protected void displayPreferenceDialog(Fragment fragment, String key) {
displayPreferenceDialog(fragment, key, null);
}
protected void displayPreferenceDialog(Fragment fragment, String key, Bundle bundle) {
Bundle b = bundle == null ? new Bundle(1) : bundle;
b.putString("key", key);
if (fragment != null) {
fragment.setArguments(b);
if (fragment instanceof DialogFragment) {
fragment.setTargetFragment(this, 0);
Copy link

@zhanghai zhanghai Nov 9, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is going to be merged, can you move fragment.setTargetFragment(this, 0) out of the instanceof DialogFragment if so that all fragments get a reference to the preference fragment? See #87 (comment) .

((DialogFragment) fragment).show(this.getFragmentManager(), FRAGMENT_DIALOG_TAG);
} else {
this.getFragmentManager()
.beginTransaction()
.add(fragment, FRAGMENT_DIALOG_TAG)
.commit();
}
}
}
Expand Down