I have this question when I am studying an open source Android project.
I have an activity A1 that calls another activity A2 in its "onCreate" method. However, there are code statements before and after the "startActivity / startActivityForResult" method. I do not know the execution sequence in A1 after the "startActivity / startActivityForResult" method.
The code framework for A1 is listed below :
public void onCreate(Bundle bundle) {
<code statements before>
this.startActivityForResult(.....); // Start A2 here.
<code statements after>
}
public void onResume() {
<code statements>
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
<code statements>
}
.....
I wonder whether A1 would firstly finish its execution cycle (i.e. "onCreate" -> "onStart" -> "onResume" -> "onPause" -> "onStop") before it would call A2, or just firstly finish "onCreate" before calling A2, or has other execution logic ?
Moreover, when A2 returns (finishes) and "onActivityResult" in A1 is called, would A1 then executes "onRestart" -> "onStart" -> "onResume" after executed the "onActivityResult" method ?
Because both A1 and A2 have its own UI, I think that the 2 activities may not execute concurrently with two different threads. Does anyone know the exact execution sequence in this case ?
Thanks in advance for any suggestion.
I have simulated this situation using 2 very simple Android activities and got the results below.
The followings are what would happen when Android executes "startActivity" / "startActivityForResult" inside a method of an activity A1 to start another activity A2,
I write it down here hoping that it may be helpful to others.
They will not execute on different threads, both will be on the main UI thread. You can't guarantee when/if onResume() and onPause() will be called as described in the Android lifecycle docs here.
If you're starting another activity in the onCreate() of an activity, you should expect that the A1 UI is not loaded (the first time). You should expect to see it when A2 returns a result.
Other than that, I would suggest just trying it out and seeing for yourself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With