Aexyn
Aexyn

Reputation: 1222

How startActivityForResult works?

So I have two classes. What I am trying to do is to send data from MainActivity to Main2, get a log in Logcat, and get control back to MainActivity so as to repeat the step again with some other data.

But I am getting Unexpected result. Instead of getting 0,1,2,3,4, I get any random sequence.. sometimes in reverse order . Why is that?? Is this due to use of for loop??

MainActivity.java

public class MainActivity extends Activity {

int REQUEST_CODE = 0;
Intent i;
int k = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    i = new Intent(this, main2.class);
    loop();
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        loop();
    }
}

public void loop() {
    for (int j = k; j < 5; j++, k++) {
        i.putExtra("value", j);
        startActivityForResult(i, REQUEST_CODE);
    }
}
}

Main2.java

public class main2 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    int i = intent.getIntExtra("value", 0);
    Log.d("Value", i + "");
    setResult(RESULT_OK);
    finish();
}  

}

Upvotes: 1

Views: 1546

Answers (3)

Al&#233;cio Carvalho
Al&#233;cio Carvalho

Reputation: 13657

The usage of startActivityForResult is for a specific need, for instance, your MainActivity needs to fetch a value that is only available on Main2 to retrieve this value. An example of usage, you have an activity A is a screen to write an email and it has a field to enter the contact information. The activity B only displays a list of contacts.

In this case, you basically will do the following:

  1. on Activity A: You will call startActivityForResult(, )
  2. on Activity B: You display the list of the contacts and wait for the user's click, on the onClick of the contact, you basically will get the contact's email. With the user email, you put it new Intent and call the setResult(RESULT_OK, intentData);
  3. on the onActivityResult() you will put code expecting the call for result == RESULT_OK and request code == X...when it comes, then you will read out the parameters from the given intent on the onActivityResult method, it will email and all the parameters you've passed on the intent of the previous step.

and that's it! good luck!

Upvotes: 0

hasan
hasan

Reputation: 24185

You are starting all of your activities from the same thread. There for they will not start before loop() method finish. for the 1st time all the 5 activities will start together. and there is no grantee that they will take same time to start and finish. so this output is normal.

Also, even if you started each activity in a different thread there is no grantee too that the 1st started activity will finish first.

All this depend on CPU processing.

What you can do is get rid of the loop. and do some logic to start the activity after the response of the last activity in onActivityResult method.

example:

public void loop() {
    j = k;
    i.putExtra("value", j);
    startActivityForResult(i, REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (resultCode == RESULT_OK)
    {
        j++;
        if (j<5)
        {
            i.putExtra("value", j);
            startActivityForResult(i, REQUEST_CODE);
        }
        else
        {
            k++;
            loop();
        }
    }
}

I am not sure. but I think this is exactly what you want. this may take a little more time not sure.

Upvotes: 0

Levente Kurusa
Levente Kurusa

Reputation: 1866

It's because, when you call startActivityForResult, you start the Activity, and it will eventually start. Note, not immediately, and it might take a few milliseconds for them to start. Also, Log.d is also not instant. Multithreaded programming differs very much from singlethreaded.

Upvotes: 1

Related Questions