当前活动解除AlertDialog后返回上一个活动

时间:2015-03-04 13:44:38

标签: android android-intent android-alertdialog

我从AccountManager开始,然后转到AccountDetails并从那里致电ShowEditDialog,并在我解雇AccountManager后返回ShowEditDialog。我已尝试在AlertDialog的Intent中使用onDismiss,但它似乎无法正常工作。

基本上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();

}

1 个答案:

答案 0 :(得分:3)

尝试拨打

finish();
设置结果后

所以,基本上

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