emrerme
emrerme

Reputation: 31

How to use activity on result?

            String s1 = e1.getText().toString();
            String s2 = e2.getText().toString();
            String s3 = e3.getText().toString();

            Intent updateIntent = new Intent(DetailActivity.this,MainActivity.class);
            Bundle bundle = new Bundle();
            bundle.putString("name", s1);
            bundle.putString("lat", s2);
            bundle.putString("lon", s3);

            updateIntent.putExtras(bundle);

this is my first class where I put the datas. I sended data the second class that i will use soon to first class that i am using in codes. Now I must use these bundle datas in my first class again. There is an ArrayList item that i put datas from JSON. I must change these data on my first class again. I hope that I explain my problem.

Upvotes: 0

Views: 154

Answers (1)

Emil Adz
Emil Adz

Reputation: 41129

To use activity for result you should first start it in your first class:

startActivityForResult(intent, 1);

in second class you need to set result and finish the activity:

Intent i = getIntent();
String msg = i.getStringExtra("color");
if (msg.contentEquals("choosecolor")) 
{
    i.putExtra("chosencolor", color);
    setResult(RESULT_OK, i);
    finish();
}

and in the first class you can receive the data using this code:

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

     if (resultCode == RESULT_OK) {
         //get the data sent from the second class here...
         data = data.getStringExtra("key");
         //do something with my precious data
     }
 }

Upvotes: 1

Related Questions