Search code examples
javaandroidandroid-activityonactivityresult

I am not able to send data from first activity (MapActivity) to the second activity (NewContact)


The problem is I am not able to send data from activity 2 to activity 1. Here is the snapshot for activity 1 (NewContact.java)

click here to see snapshot of first activity

In first activity (NewContact) At the bottom I have a TextView, I have implemented an OnClickListener on that TextView. As the user clicks on this TextView, Second Activity (MapActivity) will open.

click here to see snapshot of second activity

The user needs to search for the address and at the bottom a circular check button will appear, if the user clicks on that button then an alert dialog will appear which will ask to add the location or not. If the user clicks on ADD then the same location will be saved to TextView of FirstActivity which is NewContact.

activity_new_contact.xml

This is the ID for the TextView

android:id="@+id/etUserAddress"

NewContact.java

TvAddress = (TextView)findViewById(R.id.etUserAddress);

   Intent intent = getIntent();
   String addText = intent.getStringExtra(MapActivity.EXTRA_TEXT);
   TvAddress.setText(addText);

TvAddress.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            Intent intent = new Intent(NewContact.this, MapActivity.class);
            startActivityForResult(intent,1);
        }
    });

I also implemented the requestCode in onActivityResult in the first activity (NewContact) which is at the bottom in this function:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == REQUEST_CODE_GALLERY && resultCode == RESULT_OK)
    {
        Uri imageUri = data.getData();
        CropImage.activity(imageUri)
                .setGuidelines(CropImageView.Guidelines.ON)
                .setAspectRatio(1, 1)
                .start(this);
    }

    if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE)
    {
        CropImage.ActivityResult result = CropImage.getActivityResult(data);
        if (resultCode == RESULT_OK)
        {
            Uri resultUri = result.getUri();
            mImageView.setImageURI(resultUri);
        } else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
        {
            Exception error = result.getError();
        }
    }
    super.onActivityResult(requestCode, resultCode, data);

         //this requestCode is to send the data from one activity to other
    if (requestCode == 1)
    {
        if (resultCode == RESULT_OK)
        {
            String myStr = data.getStringExtra("EXTRA_TEXT");
            TvAddress.setText(myStr);
        }
    }

}

MapActivity.java

In second activity I also created a public static String:

public static final String EXTRA_TEXT = 
"com.example.sahil.phonebook.EXTRA_TEXT";

Below is the code for second activity, when the user selects a location then an alert dialog will be displayed, asking the user to ADD or CANCEL. If the user clicks on ADD button then the function sendDataBack(); will execute:

MapActivity.java

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

            AlertDialog.Builder a_builder = new AlertDialog.Builder(MapActivity.this);
            a_builder.setMessage("Add Location?").setCancelable(false).setPositiveButton("Add", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which)
                { //if user clicks on yes button then this will implement

                    sendDataBack();

                }
            })
                    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });

            AlertDialog alertDialog = a_builder.create();
            alertDialog.setTitle("Confirm Location");
            alertDialog.show();

        }
    });

Below is the code for the function sendDataBack();

  public void sendDataBack()
 {
  String addText = mSearchText.getText().toString();

  Intent intent = new Intent(MapActivity.this, NewContact.class);
  intent.putExtra(EXTRA_TEXT, addText);

 setResult(1,intent);
  }

Solution

  • you need to pass you value in any variable like below with intent.

    public void sendDataBack()
    {
      String addText = mSearchText.getText().toString();
    
      Intent intent = new Intent(MapActivity.this, NewContact.class);
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
      intent.putExtra("textValue", addText);
      startActivity(intent);
    
     }
    

    Access that intent on next NewContact activity.

    String addText  = getIntent().getStringExtra("textValue");