Skip to content

Instantly share code, notes, and snippets.

@Ioane5
Last active June 23, 2016 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ioane5/ce729266a5719957db8be8ead81f97d4 to your computer and use it in GitHub Desktop.
Save Ioane5/ce729266a5719957db8be8ead81f97d4 to your computer and use it in GitHub Desktop.
This is simple class for creating Persistent dialogs
package ge.example.yourPackageName
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StringRes;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
/**
* This is general persistent dialog fragment, that is recreated after rotation changes.
* <p/>
* Created by ioane5 on 6/22/16.
*/
public class PersistentDialogFragment extends DialogFragment {
public static final String TAG = PersistentDialogFragment.class.getSimpleName();
private static final String ARG_REC_CODE = "ARG_REC_CODE";
private static final String ARG_TITLE = "ARG_TITLE";
private static final String ARG_MESSAGE = "ARG_MESSAGE";
private static final String ARG_POSITIVE_TEXT = "ARG_POSITIVE_TEXT";
private static final String ARG_NEGATIVE_TEXT = "ARG_NEGATIVE_TEXT";
private static final String ARG_IS_CANCELLABLE = "ARG_IS_CANCELLABLE";
private PersistentDialogListener mListener;
public static PersistentDialogFragment newInstance(int requestCode, String title, String msg, String positiveBtnText, String negativeBtnText, boolean isCancellable) {
Bundle args = new Bundle();
args.putInt(ARG_REC_CODE, requestCode);
args.putString(ARG_TITLE, title);
args.putString(ARG_MESSAGE, msg);
args.putString(ARG_POSITIVE_TEXT, positiveBtnText);
args.putString(ARG_NEGATIVE_TEXT, negativeBtnText);
args.putBoolean(ARG_IS_CANCELLABLE, isCancellable);
PersistentDialogFragment fragment = new PersistentDialogFragment();
fragment.setArguments(args);
return fragment;
}
public static PersistentDialogFragment newInstance(Context context, int requestCode, @StringRes int msg, @StringRes int positiveBtn, @StringRes int negativeBtn) {
return newInstance(requestCode,
null,
context.getString(msg),
context.getString(positiveBtn),
context.getString(negativeBtn),
true);
}
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
Bundle args = getArguments();
builder.setTitle(args.getString(ARG_TITLE));
builder.setMessage(args.getString(ARG_MESSAGE));
if (args.containsKey(ARG_POSITIVE_TEXT)) {
builder.setPositiveButton(args.getString(ARG_POSITIVE_TEXT), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogPositiveClicked(getArguments().getInt(ARG_REC_CODE));
}
});
}
if (args.containsKey(ARG_NEGATIVE_TEXT)) {
builder.setNegativeButton(args.getString(ARG_NEGATIVE_TEXT), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
mListener.onDialogNegativeClicked(getArguments().getInt(ARG_REC_CODE));
}
});
}
setCancelable(args.getBoolean(ARG_IS_CANCELLABLE, false));
return builder.create();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
mListener = (PersistentDialogListener) context;
} catch (ClassCastException e) {
throw new ClassCastException(context.toString()
+ " must implement PersistentDialogListener");
}
}
public interface PersistentDialogListener {
void onDialogPositiveClicked(int requestCode);
void onDialogNegativeClicked(int requestCode);
}
}
// Usage
PersistentDialogFragment.newInstance(
getBaseContext(),
RC_REQUEST_CODE,
R.string.message_text,
R.string.positive_btn_text,
R.string.negative_btn_text)
.show(getSupportFragmentManager(), PersistentDialogFragment.TAG);
// Or
PersistentDialogFragment.newInstance(
getBaseContext(),
RC_REQUEST_CODE,
"Dialog title",
"Dialog Message",
"Positive Button",
"Negative Button",
false)
.show(getSupportFragmentManager(), PersistentDialogFragment.TAG);
public class ExampleActivity extends Activity implements PersistentDialogListener{
@Override
void onDialogPositiveClicked(int requestCode) {
switch(requestCode) {
case RC_REQUEST_CODE:
break;
}
}
@Override
void onDialogNegativeClicked(int requestCode) {
switch(requestCode) {
case RC_REQUEST_CODE:
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment