0

I am making a chat application In which I have provided option to upload the file on to server,issue is I want to send data to previous page.Here is what exactly what I want. enter image description here

Above is my first activity when I click on upload new activity starts enter image description here

Now when I click on"Click to upload"button image is uploaded and my second activity closes. I want to send the path of image uploaded along with image name to the previous activity's text box next to send button so how should I do it. I cannot call the activity again as it is selective chat screen from list of chat members,so I am just finishing the second activity by which my previous activity is brought to front

Please help me

Sanket Naik
  • 173
  • 2
  • 16
  • 2
    You can use `startActivityForResult` from 1st activity and in second activity finish method you can pass the uri path in intent as extra parameter. see this one [3.3. Retrieving result data from a sub-activity](http://www.vogella.com/tutorials/AndroidIntent/article.html) –  Mar 19 '14 at 04:46

3 Answers3

3

In this case you should use startActivityForResult() to launch your child(first) Activity.

This provides a pipeline for sending data back to the main Activity using setResult(). The setResult() method takes an int result value and an Intent that is passed back to the calling Activity.

Intent resultIntent = new Intent();
resultIntent.putExtra("IMAGE_PATH",path);
setResult(Activity.RESULT_OK, resultIntent);
finish();

To access the returned data in the calling Activity override onActivityResult(). The requestCode corresponds to the integer passed in in the startActivityForResult() call, while the resultCode and data Intent are returned from the child(first) Activity.

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  switch(requestCode) {
    case (MY_CHILD_ACTIVITY) : {
      if (resultCode == Activity.RESULT_OK) {
        // TODO Extract the path of image uploaded returned from intent
      }
      break;
    } 
  }
}
Gopal Gopi
  • 11,101
  • 1
  • 30
  • 43
Santhosh
  • 1,867
  • 2
  • 16
  • 23
  • MY_CHILD_ACTIVITY is keyword? I have written it code as upload.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { Intent i = new Intent(Messaging.this, MainActivity.class); //messageText.setText("uploads/"); startActivityForResult(i,1); // TODO Auto-generated method stub } }) – Sanket Naik Mar 19 '14 at 05:08
  • MY_CHILD_ACTIVITY is nothing but FIRST_ACTIVITY_REQUEST_CODE and replace startActivityForResult(i,FIRST_ACTIVITY_REQUEST_CODE); – Santhosh Mar 19 '14 at 05:13
1

You can simply reach out for this by starting your second activity with startActivityForResult() instead of startActivity() and then when you are going to finish your second activity add whatever data you want in the intent and then you will find method onActivityResult called. for more details check this example http://www.vogella.com/tutorials/AndroidIntent/article.html

Ahmed Zayed
  • 2,165
  • 1
  • 20
  • 21
1

There are more than one way to accomplish this.

The first one and the convenient one would be to use startActivityForResult() method in your 1st activity. And when you finish your secondActivity put the uri path in intent as a key Value pair. In first activity override protected void onActivityResult() method. and get the data you put in the intent.

The second way is the same start your activity as you normally start like use startActivity(your second activity) and override the finish method of secondActivity put the uri path as extra in your intent and while you finish this activity get the data in your firstActivity via overriding the onResume() method.

For more you can see this link Android Intents - Tutorial

and this one How to pass a value from one Activity to another in Android?.

Hope that helps.

Community
  • 1
  • 1