1

I have a class that extends AppCompatActivity. Within this activity I do two different calls to two startActivityForResult methods which are hooked to two button listeners. The first one calls a camera intent like so:

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);

and the second one calls another AppCompatActivity which holds a Google Map to wit:

Intent getLatLongIntent = new Intent(Form.this, MapsLatLongActivity.class);
startActivityForResult(getLatLongIntent, LATLONG_REQUEST);

The first startActivityForResult works fine and I can manipulate the response from the camera intent in the onActivityResult method. What is puzzling is that the same onActivityResult does not get triggered when I close the child activity of the second call using this code:

Intent intent = new Intent();
intent.putExtra("brdgHouseLat", latitude);
intent.putExtra("brdgHouseLong", longitude);
setResult(RESULT_OK, intent);
finish();

Below is my code for the onActivityResult method:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        himgres.setImageBitmap(photo);
    } else if (requestCode == LATLONG_REQUEST && resultCode == Activity.RESULT_OK) {
        latitude = data.getDoubleExtra("brdgHouseLat", 0);
        longitude = data.getDoubleExtra("brdgHouseLong", 0);
        Log.v(TAG, "and form has a lat = " + latitude);
        Log.v(TAG, "and a long = " + longitude);
    }
}

Needless to say I have goggled for solutions on this and have checked into my manifest (I haven't set any flags); fragments (no fragments here), etc.

Am starting to think that maybe SharedPreferences will be a better option but am just puzzled of the inconsistency of the responses. Any help will be appreciated

========================================================================= Here are the three methods of the second activity relevant to this question:

@Override
public void onBackPressed() {
    super.onBackPressed();
    packLatLong();
}

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
        case android.R.id.home: {
            packLatLong();
        }
    }
    return (super.onOptionsItemSelected(menuItem));
}

private void packLatLong() {
    if (latitude != 0 && longitude != 0) {
        Log.v(TAG, "packing this lat = " + latitude);
        Log.v(TAG, "packing this long = " + longitude);
        Intent intent = new Intent();
        intent.putExtra("brdgHouseLat", latitude);
        intent.putExtra("brdgHouseLong", longitude);
        setResult(RESULT_OK, intent);
        finish();
    }
}

Please note that pressing either the back button as well as the home button in the action bar triggers the packLatLong method

Marka A
  • 258
  • 5
  • 15
  • we need to see the complete code to verify the issue, specifically of Activity where you are creating intent for `brdgHouseLat` – Pavneet_Singh Nov 06 '17 at 06:42
  • Thanks @Pavneet_Singh but I have tried to include all relevant codes for this case. Please let me know what more codes are needed for me to be more clear. – Marka A Nov 06 '17 at 06:45
  • of Activity where you are doing `intent.putExtra("brdgHouseLat", latitude);` – Pavneet_Singh Nov 06 '17 at 06:46
  • Oh, I call this under a refactored method. Here: private void packLatLong() { if (latitude != 0 && longitude != 0) { Log.v(TAG, "packing this lat = " + latitude); Log.v(TAG, "packing this long = " + longitude); Intent intent = new Intent(); intent.putExtra("brdgHouseLat", latitude); intent.putExtra("brdgHouseLong", longitude); setResult(RESULT_OK, intent); finish(); } } – Marka A Nov 06 '17 at 06:49
  • Sorry can't seem to format that one in the comment... But yeah its a method that I call somewhere. And I made sure that it gets called too (those are what the logs are for). – Marka A Nov 06 '17 at 06:51
  • you need to post the complete code of activity by editing your post – Pavneet_Singh Nov 06 '17 at 06:56
  • Hi @Pavneet_Singh thanks for the help I have added 3 more methods that are relevant to this question. The complete code for the activity contains other methods that I think has nothing to do with the inconsistent responses of onActivityResult. – Marka A Nov 06 '17 at 07:10
  • are you pressing the home button go back? – Pavneet_Singh Nov 06 '17 at 07:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/158291/discussion-between-marka-a-and-pavneet-singh). – Marka A Nov 06 '17 at 07:22
  • if (requestCode == LATLONG_REQUEST && resultCode == Activity.RESULT_OK)... You may be check this condition resultcode == Ok. – Mohamed Mohaideen AH Nov 06 '17 at 07:44
  • That's not it at all @MohamedMohaideenAH as the onActivityResult itself is not triggered. All other conditions following have no effect. – Marka A Nov 06 '17 at 07:54
  • If onActivityResult not triggerred.. Check this condition are true. if (latitude != 0 && longitude != 0) { in second activity. – Mohamed Mohaideen AH Nov 06 '17 at 07:56

1 Answers1

0

I ended up solving this problem with some hints from @Pavneet_Singh:

Here is the solution:

In the method:

@Override
public void onBackPressed() {
    super.onBackPressed();
    packLatLong();
}

remove the call to super as it always returns a RESULT_CANCEL even if you set it to RESULT_OK.

This answer helped me on this -- https://stackoverflow.com/a/35241952/1637525.

I also changed

setResult(RESULT_OK, intent);

to

setResult(Activity.RESULT_OK, intent);
Marka A
  • 258
  • 5
  • 15