【问题标题】:Is there a way to finish the current activity with a button of an alert dialog?有没有办法通过警报对话框的按钮来完成当前活动?
【发布时间】:2019-11-08 13:47:24
【问题描述】:

我创建的警告框有问题。我的意图是使用警报框重新启动或完成我的更改活动页面的活动。但是,finish(); 不起作用并会出现波浪线错误。

有没有办法在没有错误的情况下使用警报框的按钮完成当前活动?

CreateCancelDialog.java

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
    }
}

编辑: 我知道下面的代码在正常活动中工作。但它在警报对话框中不起作用。

Intent intent = new Intent(this, ChangePassword.class);
finish();
startActivity(intent);

【问题讨论】:

    标签: android android-fragments android-intent


    【解决方案1】:

    尝试调用: getActivity().finish(); 而不仅仅是 finish();

    【讨论】:

      【解决方案2】:

      我设法解决了这个问题...... 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();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-29
        • 2020-06-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-03
        相关资源
        最近更新 更多