3

I'm running into problems with the Android lifecycle. As far as I know this isn't covered in the documentation.

I have Activity A and it starts Activity B with a startActivityForResult.

public class ActivityA extends Activity {
/** Called when the activity is first created. */

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    Button b = (Button)findViewById(R.id.btnStart);
    b.setOnClickListener(new OnClickListener(){

        public void onClick(View v) {

                startActivityForResult(new Intent(ActivityA.this, ActivityB.class), 0);

        }});
}    
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.e("ActivityA", "onResult");     
}
}

Activity B calls the gallery activity:

public class ActivityB extends Activity {
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Log.e("ActivityB", "onResult");     
    setResult(0, data);
    finish();   
}
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);      
    Button b = (Button)findViewById(R.id.btnStart);
    TextView v = (TextView)findViewById(R.id.helloText);
    v.setText("Activity B");
    b.setOnClickListener(new OnClickListener(){
        public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent, 0);

        }});     
}       
}

When the activities A and B stay in memory, the results are poped back down the Activity stack just as expected.

The problem is that the gallery activity can consume enough resources to push Activity A and Activity B out of memory. Android kills the process. When the gallery activity returns a result, Activity A and B are created again and the onActivityResult for activityB is called. However, this time when ActivityB calls finish(), the onActivityResult for the newly created ActivityA is not called.

Am I doing something wrong?
Is this a bug?
Is this expected Android behavior?

Thanks!

jayd16
  • 54
  • 4

2 Answers2

1

This isn't a bug. The 2nd time that Activity 2 starts it isn't started by Activity A. When Activity B gets killed you need to save the appropriate state in the onStop, onPause, or onDestroy methods and then use that state when the activity restarts. Using that information you can still pass the appropriate information back to Activity A when Activity B quits.

slayton
  • 20,123
  • 10
  • 60
  • 89
  • So Android will only do this automatically for the parent of the foreground activity? You say that its "not started by A" but isn't it still started by the Intent created by A? – jayd16 Oct 31 '11 at 21:21
  • Thinking about your answer some more, "Using that information you can still pass the appropriate information back to Activity A when Activity B quits." How am I supposed to do that if Activity A never gets a result from B? – jayd16 Oct 31 '11 at 21:48
  • check out the answer to this question, http://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity – slayton Oct 31 '11 at 23:22
0

So this seems like a bug with cyanogenmod. I can easily reproduce this issue on my Droid with CyanogenMod-7.0.3-Droid but after running through a few devices with stock OS and the emulators it seems like this is only on my Cyanogen Droid. Very frustrating realization.

jayd16
  • 54
  • 4