简体   繁体   中英

onActivityResult() is not calling from Activity

I'm able to edit & save data from one Activity(EditActivity.java), but the updated data doesn't display(or carried over) to the next Activity(ViewActivity.java) when click Save button. I can see the changes on the EditText fields if go back to the EditActivity page.

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

    }

Your headline says that, that onActivityResult() does not get called.

Use startActivityForResult(in, 55) instead of startActivity(in)

//EDIT: By the way, checking the result code should by done using the RESULT_OK / RESULT_CANCELED constants. You might also consider checking the request code (in my example code it would be 55)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM