Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android --- how will startActivity or startActivityForResult affect the logic flow of the current activity?

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.

like image 671
user1129812 Avatar asked Dec 09 '22 00:12

user1129812


2 Answers

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,

  1. it would recognize that there is now an intent to fire another activity A2,
  2. it would execute to the end of the current method (it would NOT jump directly to the intented activity A2),
  3. it would then call the "onPause" method of the current activity A1 (to allow us to save the current state) and then leave the current activity,
  4. it would now start the intented activity A2 and call its "onCreate" -> "onStart" -> "onResume" method sequence,
  5. the "onStop" method of the initial calling activity A1 MAY be called here if the called activity A2 has covered the whole screen,
  6. the "onDestroy" method of the initial calling activity A1 MAY be called here if the called activity A2 is a "singleTask" activity and A1 is on top of A2 in the stack (all activities on top of a "singleTask" activity would be destroyed when the "singleTask" activity is brought to the front).

I write it down here hoping that it may be helpful to others.

like image 168
user1129812 Avatar answered May 14 '23 12:05

user1129812


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.

like image 24
JonnyBoy Avatar answered May 14 '23 13:05

JonnyBoy