0

I am trying to let the user select shipping address through google maps. The coordinates selected needs to be displayed in a text view. I'm using intents to return the selected coordinates to the previous activity but the problem is that no matter what I try the intent is returning null every time.

This is button click event that should put the coordinates inside the intent:

AddressSelectActivity.java

confirm.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent();
            intent.putExtra("Latitude", latitude);
            setResult(RESULT_OK, intent);

            finish();
        }
    });

I'm retrieving the values onResume():

Register.java

shipping.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent mapsActivity = new Intent(v.getContext(), AddressSelectActivity.class);
            startActivityForResult(mapsActivity, 1);

        }
    });

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Toast.makeText(this, "Inside onActivityResult", Toast.LENGTH_SHORT).show();
    if (requestCode == 1) {
        if(resultCode == RESULT_OK) {
            Toast.makeText(this, data.getStringExtra("Latitude"), Toast.LENGTH_SHORT).show();
            shipping.setText(data.getStringExtra("Latitude"));
        }
    }
}

Can anyone provide any guidance. Thank You!

1 Answers1

1

You are not sending Intent anywhere, you just create Intent and call finish(). Even if you would send it, by calling startActivity, it would create another instance of Register activity. I suppose you do not want achieve that. To send data backwards check this post: How to pass data from 2nd activity to 1st activity when pressed back? - android

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
Szymon Gajdzica
  • 154
  • 1
  • 2
  • 11
  • Thanks. I tried following the steps given in the link you attached. You can see the changes I made. Unfortunately, problem still persists. The function onActivityResult is getting called but the "Latitude" it is returning is still null. What should I do :( –  Jun 02 '20 at 10:30
  • 1
    You are passing Double to Intent and trying to get String. You should use data.getDoubleExtra() – Szymon Gajdzica Jun 02 '20 at 11:57
  • Can't believe I overlooked that. Thank You very much. Problem resolved!! –  Jun 02 '20 at 12:29