有没有一种方法可以通过警报对话框的按钮来完成当前活动?

时间:2019-11-08 13:47:24

标签: android android-fragments android-intent

我创建的警报框出现问题。我的意图是使用警报框重新启动或完成“更改活动”页面的活动。但是,使用bool compare_string(const char *string1, const char *string2) { int i = 0, j = 0, result = 0; while (string1[i] != '\0') { i++; } while (string2[j] != '\0') { j++; } i = 0; j = 0; while ((string1[i] != '\0') && (string2[j] != '\0')) { if (string1[i] < string2[j]) { result = -1; break; } else if (string1[i] > string2[j]) { result = 1; break; } else if (string1[i] == string2[j]) { result = 0; } i++; j++; } if ((result == 0) && (i==j)) { printf("identical strings \n"); } else if (result == -1) { printf("not identical, -1 \n"); } else if (result == 1) { printf("not identical, 1 \n"); } } //in main char str_compare1[STRING_LIMIT]; char str_compare2[STRING_LIMIT]; printf("enter 1st string to compare, (100) characters or less: \n"); fgets(str_compare1, STRING_LIMIT, stdin); printf("enter 2nd string to compare, (100) characters or less \n"); fgets(str_compare2, STRING_LIMIT, stdin); result = compare_string(str_compare1, str_compare2); 无效,并且会产生混乱的行错误。

有没有一种方法可以使用警报框的按钮来完成当前活动,而不会出现错误?

CreateCancelDialog.java

finish();

编辑: 我确实知道以下代码可以正常进行。但是它在警报对话框中不起作用。

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AppCompatDialogFragment;

public class CreateCancelDialog extends AppCompatDialogFragment {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        AlertDialog.Builder builderDialogCancel = new AlertDialog.Builder(getActivity());
        builderDialogCancel.setTitle("Confirm Cancellation");
        builderDialogCancel.setMessage("Are you sure you wish to return to the main screen?");
        builderDialogCancel.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                callMainPage();
            }
        }) .setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                getDialog().cancel();
            }
        });

        return builderDialogCancel.create();
    }

    private void callMainPage() {
        Intent intent = new Intent(getActivity(), GPIMainUI.class);
        startActivity(intent);
        finish(); //gives out squiggly line
    }
}

2 个答案:

答案 0 :(得分:1)

尝试致电: getActivity().finish();而不只是finish();

答案 1 :(得分:0)

我设法解决了这个问题... Webfreak为我提供了解决问题的线索。归功于Webfreak。

builderDialogCancel.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
            callMainPage(getActivity());
        }
    })

private void callMainPage(Context context) {
    Intent intent = new Intent(getActivity(), GPIMainUI.class);
    startActivity(intent);
    ((Activity)context).finish();
}