0

Do Activity lifecycle callbacks like onCreate(), onStart(), onResume(), always wait for the former to finish before executing, or is it possible that onStart() gets called before onCreate() finishes, interrupts it, and then onCreate continues after onStart() finishes. Is it even possible that they run concurrently, like can onStart() get called while onCreate() hasn't finished, and then a line of onStart() executes, and then a line of onCreate(), and so on.

I assume this isn't the case, and logging on my app shows it isn't, but I haven't ever seen it mentioned explicitely, because I can't know what it's like on other versions, or devices. I also read that each app gets one thread by default, so I think that means that it can't be concurrent, but I wanna make sure.

The main thing that got me thinking about this is the fact that some people online say that in an Activity onCreateOptionsMenu() is called after onCreate() starts but before it finishes. For an example the first answer here. But another user said somewhere else that on some versions onCreateOptionsMenu() is called inside setContentView(), and I guess that's why that is. So does it go something like this then?

onCreate() -> onCreateOptionsMenu() called, onCreate() paused -> onCreateOptionsMenu() finishes -> onCreate() is resumed

Did I get this right? There is no concurrency involved?

And something like this can't happen for the main callbacks like onCreate(), onStart(), onResume()?

  • They are all executed on main application thread in order. `onCreateOptionsMenu` is not a lifecycle related method as mentioned in question you linked, it's related to menu being shown/opened. – Pawel Feb 16 '21 at 20:59
  • @Pawel, thanks for answering, so will onStart() only be called after onCreate() finishes? – TwoheadedFetus Feb 16 '21 at 21:22
  • Yes, `onStart()` will only be called after `onCreate()` finishes. – David Wasser Feb 16 '21 at 21:34
  • @DavidWasser thanks for clarifying, the the first answer here got me confused https://stackoverflow.com/questions/58899219/why-does-onstart-method-run-before-oncreate-in-my-android-project. The wording made me question it. I imagine its because hes making an asynchronous call in it. – TwoheadedFetus Feb 16 '21 at 23:36

1 Answers1

2

You are referencing very old questions/answers regarding the timing of onCreateOptionsMenu(), and these very old posts refer to ActionBarSherlock which was a library that was not part of the Android framework (and may not have followed the rule that I describe below).

As far as I know, all calls to framework methods on Activity or Fragment (unless specifically documented as otherwise) are made on the main (UI) thread. This means that they cannot be interrupted by any other framework calls, that they run to completion and that they cannot run in parallel. My own personal empirical analysis agrees.

David Wasser
  • 93,459
  • 16
  • 209
  • 274