Java tutorial
/* * Copyright (c) 2016, Sergey Penkovsky <sergey.penkovsky@gmail.com> * * This file is part of TraccarLitvakM (fork Erlymon Monitor). * * TraccarLitvakM (fork Erlymon Monitor) is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TraccarLitvakM (fork Erlymon Monitor) is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with TraccarLitvakM (fork Erlymon Monitor). If not, see <http://www.gnu.org/licenses/>. */ package org.erlymon.litvak.monitor.view.fragment; import android.app.Activity; import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v7.app.AlertDialog; import org.erlymon.litvak.monitor.R; /** * Created by Sergey Penkovsky <sergey.penkovsky@gmail.com> on 26.10.15. */ public class ConfirmDialogFragment extends DialogFragment { public static ConfirmDialogFragment newInstance(int titleId, int messageId) { ConfirmDialogFragment newFragment = new ConfirmDialogFragment(); Bundle args = new Bundle(); args.putInt("titleId", titleId); args.putInt("messageId", messageId); newFragment.setArguments(args); return newFragment; } /* The activity that creates an instance of this dialog fragment must * implement this interface in order to receive event callbacks. * Each method passes the DialogFragment in case the host needs to query it. */ public interface ConfirmDialogListener { void onPositiveClick(DialogInterface dialog, int which); } // Use this instance of the interface to deliver action events ConfirmDialogListener mListener; // Override the Fragment.onAttach() method to instantiate the NoticeDialogListener @Override public void onAttach(Activity activity) { super.onAttach(activity); // Verify that the host activity implements the callback interface try { // Instantiate the NoticeDialogListener so we can send events to the host mListener = (ConfirmDialogListener) activity; } catch (ClassCastException e) { // The activity doesn't implement the interface, throw exception throw new ClassCastException(activity.toString() + " must implement ConfirmDialogFragment"); } } @Override public Dialog onCreateDialog(Bundle savedInstanceState) { AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(), R.style.AppTheme_Dialog); AlertDialog dialog = builder.setTitle(getArguments().getInt("titleId")) .setMessage(getArguments().getInt("messageId")) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if (mListener != null) { mListener.onPositiveClick(dialog, which); } } }).setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }).create(); return dialog; } }