2 slaks SLaks 于 2013.05.29 17:23 提问

子 activity 结束后不能调用 onactivity 结果函数

我使用下面的代码:

addNewReceipt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent myIntent = new Intent(v.getContext(), AddReceipt.class);
                myIntent.putExtra("receiptList", receipts);
                startActivityForResult(myIntent, RECEIPT_ADD);
            }
        });
    }
    @SuppressWarnings("unchecked")
    public synchronized void onActivityResult(final int requestCode, int resultCode, final Intent data) 
    {
    if (resultCode == Activity.RESULT_OK) 
    {
     if (requestCode == RECEIPT_ADD)
     {
        receipts = (ArrayList<Receipt>) data.getSerializableExtra("receiptList");
        addReceiptsInListView();
     }
    }
    }

AddReceipt class代码:

@Override
    public void finish() {
        Intent data = new Intent();
        data.putExtra("receiptList", receipts);
        setResult(RESULT_OK, data); 
        super.finish();
    }
    @SuppressWarnings("unchecked")
    public void onCreateCall()
    {
          done.setOnClickListener(new OnClickListener() 
          {  //receiptAddBtn
                @Override
                public void onClick(View v) 
                {
                    String error = "";
                    Receipt receipt = new Receipt();
                    receipt.comments = comments.getText().toString();
                    receipt.referenceNo = receiptNo.getText().toString();
                    receipt.image = imageSelected;
                    if (receipt.image == null || receipt.referenceNo == "" )
                    {
                        error = "Please input Receipt No. and attach Image";
                        displayAlert(error);
                    }
                    else
                    {
                        receipts.add(receiptCounter,receipt);
                                              finish();
                    }
                }
            });
    }

现在的问题是,当activity 结束后,在之前的类中我调用这个activity ,public synchronized void onActivityResult但是没有执行。

返回按钮也没有结束activity。
哪里出错呢?

1个回答

hxn_217
hxn_217   2013.05.30 10:04
已采纳

参考解决方案:

@Override
    public void finish() {
        Intent data = new Intent();
        data.putExtra("receiptList", receipts);
        setResult(Activity.RESULT_OK, data); 
        super.finish();
    }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!