9
votes

I have an application that open an other class using intent :

private void createRepository(){
        Intent j = new Intent(this, Repository.class);
        startActivityForResult(j, ACTIVITY_CREATE);
    }

In Repository.class we have the onActivityResult method :

public void onActivityResult(int reqCode, int resultCode, Intent data) {
      super.onActivityResult(reqCode, resultCode, data);

      switch (reqCode) {
        case (PICK_CONTACT) :
          if (resultCode == Activity.RESULT_OK) {
            Uri contactData = data.getData();
            c =  managedQuery(contactData, null, null, null, null);
            if (c.moveToFirst()) {
              //String name = c.getString(c.getColumnIndexOrThrow(People.NAME));
              num = c.getString(c.getColumnIndexOrThrow(People.NUMBER));

            }    

          }    
        break;
      }
      finish();
    }

I don't know how I can return the value of num to the first class (that create Repository.class). Thank you for your help. Michaël

1

1 Answers

6
votes

I think you got the directions mixed up.

In the Repository class you have to setResult() before calling finish. For additional Data you can putExtra() data.

For example, set your result in the onCreate() function.

In your calling class (the one that starts Repository) you overwrite onActivityResult(int requestCode, int resultCode, Intent data) and get data with data.getBundleExtra().

Androids reference for Intent and Activity has good descriptions and the samples also contain a ReceiveResult and SendResult sample.