在当前 activity 关闭 AlertDialog 后返回上一个 activity

Returning to previous activity after current activity dismisses AlertDialog

我从 AccountManager 开始,然后转到 AccountDetails,然后我从那里调用 ShowEditDialog,一旦我解雇 [=],我想 return 到 AccountManager 17=]。我试过在 AlertDialog 的 onDismiss 中使用 Intent 但它似乎不起作用。

所以基本上 AccountManager -> AccountsDetails - >ShowEditDialog -> AccountManager

AccountsManager

 try {
        grid.setOnItemClickListener(new OnItemClickListener() {

            @Override
                public void onItemClick(AdapterView<?> parent, View v, int position,
                                                                long id) {
                try {

                    SQLiteCursor cr = (SQLiteCursor) parent.getItemAtPosition(position);
                    String name = cr.getString(cr.getColumnIndex(DatabaseHelper.colName));
                    int amount = cr.getInt(cr.getColumnIndex(DatabaseHelper.colAmount));
                    String purpose = cr.getString(cr.getColumnIndex(DatabaseHelper.colPurpose));
                    String Terms = cr.getString(cr.getColumnIndex(DatabaseHelper.colTermsClass));
                    String Status = cr.getString(cr.getColumnIndex(DatabaseHelper.colStatClass));
                    String date = cr.getString(cr.getColumnIndex(DatabaseHelper.colDate));
                    String editdate = cr.getString(cr.getColumnIndex(DatabaseHelper.colEditDate));
                    Account acc = new Account(name, amount, purpose, db.GetTermsID(Terms),date,editdate,db.GetStatID(Status));
                    acc.SetID((int) id);


                    Intent myIntent = new Intent(AccountManager.this, AccountDetails.class);
                    myIntent.putExtra("AccountObject", acc);
                    startActivityForResult(myIntent, 0);


                    } catch (Exception ex) {
                           Alerts.CatchError(AccountManager.this, ex.toString());
                    }
                }


        });
    } catch (Exception ex) {

    }

AccountDetails(我调用 ShowEditDialog 的按钮片段)

    Button modify = (Button) findViewById(R.id.modify);
    modify.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
           AlertDialog diag = Alerts.ShowEditDialog(AccountDetails.this, Acc);
           diag.show();
           diag.setOnDismissListener(new DialogInterface.OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    Intent i = new Intent();
                    setResult(0, i);
                }
            });
        }
    });

ShowEditDialog

public static AlertDialog ShowEditDialog(final Context con,final Account Acc)
{
    AlertDialog.Builder b=new AlertDialog.Builder(con);
    b.setTitle("Account Details");
    LayoutInflater li=LayoutInflater.from(con);
    View v=li.inflate(R.layout.editdialog, null);

    b.setIcon(android.R.drawable.ic_input_get);

    b.setView(v);
    final TextView txtName=(TextView)v.findViewById(R.id.txtDelName);
    final TextView txtAmt=(TextView)v.findViewById(R.id.txtDelAmt);
    final TextView txtPurpose=(TextView)v.findViewById(R.id.txtDelPurpose);
    final Spinner spinTerm=(Spinner)v.findViewById(R.id.spinTerm);
    final Spinner spinStat=(Spinner)v.findViewById(R.id.spinStat);



    Utilities.ManageTermSpinner(con, spinTerm);
    for(int i=0;i<spinTerm.getCount();i++)
    {
        long id=spinTerm.getItemIdAtPosition(i);
        if(id==Acc.getTerms())
        {
            spinTerm.setSelection(i, true);
            break;
        }
    }

    Utilities.ManageStatSpinner(con, spinStat);
    for(int j=0;j<spinStat.getCount();j++)
    {
        long id=spinStat.getItemIdAtPosition(j);
        if(id==Acc.getStatus())
        {
            spinStat.setSelection(j, true);
            break;
        }
    }


    txtName.setText(Acc.getName());
    txtAmt.setText(String.valueOf(Acc.getAmt()));
    txtPurpose.setText(Acc.getPurpose());

    b.setPositiveButton("Modify", new OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            Acc.setName(txtName.getText().toString());
            Acc.setAmt(Integer.valueOf(txtAmt.getText().toString()));
            Acc.setPurpose(txtPurpose.getText().toString());
            Acc.setTerms((int) spinTerm.getItemIdAtPosition(spinTerm.getSelectedItemPosition()));
            Acc.setStatus((int) spinStat.getItemIdAtPosition(spinStat.getSelectedItemPosition()));
            try
            {
                DatabaseHelper db=new DatabaseHelper(con);
                db.UpdateAcc(Acc);
                Alerts.ShowAccModifiedAlert(con);
            }
            catch(Exception ex)
            {
                CatchError(con, ex.toString());
            }
        }
    });
    b.setNegativeButton("Cancel", null);

    return b.create();

}

试试打电话

finish();

设置结果后

所以,基本上

diag.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        Intent i = new Intent();
        setResult(0, i);
        finish();
    }
});