Lifecycle of Android activity is always confusing beginners. The Activity
class has defined dozens of callback functions, all begin with “on”. Some of them are triggered by UI events, some are lifecycle related. It isn’t easy for developer to remember the usage of them.
An activity has essentially four states, when some event happens, like user navigates between different activities, state transites. There are lots of callback functions which will be called by framework when an activity transites between states. So the activity lifecyle is ideally modeled in state machine. However in Android SDK document, the Activity Lifecycle is descripted by a diagram and table of callback functions. It explains the order of the callback functions being called, but a developer can’t clearly understand in whatcircumstance each callback function will be called.
I tried to draw an UML state machine diagram for Android activity lifecycle:
If you aren’t familiar with UML state machine diagram, read the introduction on Wikipedia.