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!