0

Есть код:

public class Dialog2 extends DialogFragment implements OnClickListener {

    final String LOG_TAG = "myLogs";

    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder adb = new AlertDialog.Builder(getActivity())
                .setTitle(R.string.exit)
                .setPositiveButton(R.string.yes, this)
                .setIcon(android.R.drawable.ic_dialog_info)
                .setNegativeButton(R.string.no, this)
                .setNeutralButton(R.string.cancel, this)
                .setMessage(R.string.save_data);
        return adb.create();
    }

    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case Dialog.BUTTON_POSITIVE:
                saveData();
                finish();
                break;
            case Dialog.BUTTON_NEGATIVE:
                finish();
                break;
            case Dialog.BUTTON_NEUTRAL:
                break;
        }
    }

    void saveData() {

        Toast.makeText(getActivity(), R.string.saved, Toast.LENGTH_SHORT).show();
    }

    public void onDismiss(DialogInterface dialog) {
        super.onDismiss(dialog);
        Log.d(LOG_TAG, "Dialog 2: onDismiss");
    }

    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        Log.d(LOG_TAG, "Dialog 2: onCancel");
    }
}

Хочу закрывать приложение по нажатию кнопки, но ругается на finish(). Это потому, что он находится не в MainActivity? На что его можно заменить или изменить?

1 ответ 1

2

вы не поверите, но снова getActivity().finish();

1
  • :D Спасибо. getActivity() - очень полезный метод.
    – compl
    15 авг 2014 в 15:34

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.