onActivityResult()不是从Activity调用

时间:2012-08-02 19:08:54

标签: java android

我能够编辑&保存来自一个Activity(EditActivity.java)的数据,但是当单击Save按钮时,更新的数据不会显示(或转移)到下一个Activity(ViewActivity.java)。如果返回EditActivity页面,我可以看到EditText字段的更改。

EditActivity.java

btnSave.setOnClickListener(new View.OnClickListener() {

        public void onClick(View arg0) {
            // starting background task to update request
            new SaveRequestDetails().execute();
        }
    });

    class SaveRequestDetails extends AsyncTask<String, String, String> {


    protected String doInBackground(String... args) {

        // getting updated data from EditTexts
        String request_title = txtTitle.getText().toString();
        String request_date = txtSdate.getText().toString();
        String reqEndDate = txtEdate.getText().toString();
        String hours = txtHours.getText().toString();
        String reason = txtReason.getText().toString();
        String explanation = txtExp.getText().toString();


        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair(TAG_ID, request_id));
        params.add(new BasicNameValuePair(TAG_TITLE, request_title));
        params.add(new BasicNameValuePair(TAG_SDATE, request_date));
        params.add(new BasicNameValuePair(TAG_EDATE, reqEndDate));
        params.add(new BasicNameValuePair(TAG_HOURS, hours));
        params.add(new BasicNameValuePair(TAG_REASON, reason));
        params.add(new BasicNameValuePair(TAG_EXP, explanation));


        JSONObject json = jsonParser.makeHttpRequest(url_update_request,
                "POST", params);


        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {

                Intent i = getIntent();
                // send result code 100 to notify about request update
                setResult(100, i);
                finish();
            } else {

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

ViewActivity.java

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_request);

        Intent i = getIntent();

        // getting request id (rid) from intent
        request_id = i.getStringExtra(TAG_ID);

        // Getting complete request details in background thread
        new GetRequestDetails().execute();

        btnEdit = (Button) findViewById(R.id.btnEdit);

        btnEdit.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // Starting new intent
                Intent in = new Intent(getApplicationContext(), EditActivity.class);
                // sending rid to next activity
                in.putExtra(TAG_ID, request_id);
                startActivity(in);

            }
        });

    }

    // Response from Edit Activity
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // if result code 100
        if (resultCode == 100) {
            // if result code 100 is received 
            // means user edited/deleted request
            // reload this screen again
            Intent intent = getIntent();
            finish();
            startActivity(intent);
        }

    }

1 个答案:

答案 0 :(得分:3)

您的标题是,onActivityResult()不会被调用。

使用startActivityForResult(in, 55)代替startActivity(in)

//编辑: 顺便说一句,检查结果代码应该使用RESULT_OK / RESULT_CANCELED常量完成。您也可以考虑检查请求代码(在我的示例代码中它将是55)