2

Basically: Activity A calls startActivityForResult, which launches Activity B. At this point, Activity B SHOULD call back to Activity A's onActivityResult method. Instead, Activity B then incorrectly calls back to Activity C's onActivityResult method. Note that Activity B.getCallingActivity() returns Activity A, as expected.

In more detail:

I have three Activities:

  • EditModuleActivity (Activity A)

  • EditMotorActivity (Activity B)

  • ConfigurationActivity (Activity C)

Inside of EditModuleActivity (Activity A), I have this code:

private void launchIntentFromHere(){
    int requestCode = 101;
    Intent editMotorIntent = new Intent(context, EditMotorActivity.class);     
    editMotorIntent.putExtra(EditMotorActivity.EDIT_MOTOR_CONTROLLER, item);
    editMotorIntent.putExtra("requestCode", requestCode);
    setResult(RESULT_OK, editMotorIntent);
    startActivityForResult(editMotorIntent, requestCode);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    DbgLog.error("*************************** why is this never getting called?");
}

Inside of EditMotorActivity (Activity B), I have this code:

private void doThisThing(Object item){
    // blah blah build intent
    DbgLog.error("calling activity: " + getCallingActivity().toString()); // this prints out Activity A, which is what I expect.
    returnIntent.putExtra(EDIT_MOTOR_CONTROLLER, item);
    setResult(RESULT_OK, returnIntent); 
    finish();
}

ConfigurationActivity (Activity C) has this in it:

private void launchIntent(Object item){
    Intent editMotorIntent = new Intent(context, EditMotorActivity.class);
    editMotorIntent.putExtra(EditMotorActivity.EDIT_MOTOR_CONTROLLER, item);
    setResult(RESULT_OK, editMotorIntent); 
    startActivityForResult(editMotorIntent, 1);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // deals properly with the results
}

When I am in EditModuleActivity and I launch EditMotorActivity, ConfigurationActivity.onActivityResult gets called. Instead, When I am in EditModuleActivity and I launch EditMotorActivity, EditModuleActivity.onActivityResult should get called but that's not happening.

Questions

  • Why is the wrong onActivityResult getting called??

  • How do I call the right onActivityResult? Is my expectation incorrect? How else does this work?

  • If the wrong one gets called, how to I call the correct one?

Answers I've seen that didn't help

Like this:

   B
 /  \
A    C

Not this:

X   Z
 \ /
  Y
Community
  • 1
  • 1
ncrypticus
  • 505
  • 1
  • 4
  • 16
  • 1
    It would help if you could 1) relabel your activities in "A calls B calls C" order - Right now, it sounds like it's "A calls C calls B" which is confusing 2) if you could clarify from your notes what "if you call Activity B from class A" means - is it "Class A" or "Activity A" - because a "class" call to an activity that is not also an activity is weird and may explain your results and 3) post your actual code, not paraphrased versions - you may have a bug that is not evident in your paraphrasing even if you think it is an accurate representation... maybe delete irrelevant lines, but still... – Jim Nov 13 '14 at 03:20
  • Thanks for the response! 1. I relabeled. Note that B NEVER calls C. A calls B, and C calls B, but B never calls C. 2. Clarified - it's all activities calling other activities. 3. I can't post original code :( Mostly I am hoping that someone can confirm that my understanding of what is SUPPOSED to happen is correct. – ncrypticus Nov 13 '14 at 04:21
  • how do you initialize context – Zar E Ahmer Nov 13 '14 at 05:40

3 Answers3

3

I was overriding the onStop method like this:

@Override
public void onStop(){
  setResult(RESULT_CANCELED, new Intent());
  finish();
}

Basically, I didn't understand how the Android Lifecycle worked. I thought onStop() would only be called when an Activity was KILLED, and I thought it wouldn't be killed if it was waiting for a response from another Activity.

So what was happening was this: Activity C launched Activity A, which launched Activity B. When Activity B started, A.onStop() was called. Then, when Activity B finished, it (correctly) tried to return back to Activity A, but Activity A was already Stopped, so it returned all the way back to Activity C.

ncrypticus
  • 505
  • 1
  • 4
  • 16
1

OK - most likely you have a reference to the wrong "context" or a static reference to an activity or something weird elsewhere in your code.

To directly answer your question(s):

  1. Your code doesn't explain why - you need to post more code... if possible.

  2. Your expectation is correct. You appear to clearly understand how it startActivityForResult works. YOur references also demonstrate that you know what you are doing. It does not "work" any other way... but that doesn't mean you don't have an error (see "1" above!)

  3. If you are unable to figure out what went wrong, then you can always call the Activity that you want explicitly (startActivity) passing the intent that you want and then processing that intent. You may want to research onNewIntent if you still are getting weird results in rare cases...

Jim
  • 10,172
  • 1
  • 27
  • 36
  • Thank you so much! This was super super helpful. You've given me at least three solid new things to think about. I'll write a streamlined version of my problem to isolate the issue and post all the code later today. – ncrypticus Nov 13 '14 at 14:21
  • Ok... so I pulled out the relevant code and recreated a mini version of this problem, but that version WORKED. So I added tons of prints, and tore my hair out for about a day, and suddenly it started working. I didn't have time to dig around after it started working, so I'm not 100% sure, but I THINK I found the problem. (I accepted my own answer even though that feels a little bit like bad form...). Thanks again for all of your help! – ncrypticus Nov 25 '14 at 18:59
1

If somebody else had a similar problem (when startActivityForResult() returns to wrong activity, not to the one which called it), take a look at this solution

Ildar Zaripov
  • 491
  • 7
  • 15