I was encountering the same exact problem. That is, I had an Activity A that would start an Activity B using startActivityForResult. B's orientation is locked by the manifest. A's orientation is not locked.
When I rotated my device so the orientation was different, Activity B would not be recreated. This is expected and normal. However, once I performed a UI action that would finish() Activity B, instead of seeing Activity A being reconstructed and shown on the screen, I was shown Activity B again. Additionally, Activity's B's onActivityResult was being called instead of A's onActivityResult.
I figured out what was happening. I was using the same Intent object used to launch A to also launch B. After the orientation change and after B is finished, the Android system detects that Activity A needs to be rebuilt, since A was last built when the orientation was different. The system will reconstruct A using the same Intent object initially used to build A. However, the Intent object was modified to launch B instead, so the system will reconstruct Activity B instead of Activity A.
My code followed logic similar to below:
public class A extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...omitted code for clarity...
Intent i = getIntent();
i.setClass(this, B.class);
startActivityForResult(i, 0);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//...omitted code for clarity...
}
}
And my Activity B looked like:
public class B extends Activity {
//...omitted code for clarity....
public void myFinishingClickHandler(View clickedView) {
//...omitted code for clarity...
finish();
}
}
Quick walkthrough of the scenario:
- Device is in portrait mode. User launches Activity A. System will construct Activity A with an Intent I, whose target is set to A.
- Activity A modifies its Intent I by changing the target to B.
- Activity A starts an activity with Intent I. The system will construct Activity B since I is now pointing to B.
- B is now running. User changes the device orientation to landscape.
- User clicks button whose click listener calls myFinishingClickHandler. The method calls finish() on Activity B.
- Activity B finishes. The system begins the process of restoring Activity A.
- The system notes that Activity A was constructed when the device was in portrait orientation, but now the device is in landscape mode. The system will try recreating Activity A instead. The existing Activity A is sent through its destruction flow (calling onDestroy).
- The system will reconstruct Activity A using the saved Intent I. However, I's target is still Activity B. The system instead builds Activity B instead of Activity A.
Solution: Don't share Intents for launching activities. Use the Intent copy constructor.
Also, lock your activities' orientations to worry less about orientation changes (other configuration changes will cause your activities to be reconstructed, e.g. changing languages).