Search code examples
androidandroid-lifecycle

Why does OnResume gets called when starting activity for the first time


I have an activty and when it loads the first time it does some stuff OnCreate that i also needed to run when OnResume, the problem is that the first time it comes into the activity it goes through both OnCreate and OnResume, if I navigate to another activity and then use the back arrow to go back to the activity it only triggers OnResume, which is fine except when it goes through it when the activity is being ran for the first time, its causing it to call the same thing twice. How can I avoid the activity not calling OnResume() the first time if at all possible?


Solution

  • How can I avoid the activity not calling OnResume() the first time if at all possible?

    It is not possible. It is specifically designed this way, so that you don't do things twice.

    when it loads the first time it does some stuff OnCreate that i also needed to run when OnResume

    Move all of that "stuff" into onResume(). That way, the work is done regardless of whether the activity is first coming onto the screen or is returning to the screen.

    You may wish to spend some time reviewing the activity lifecycle.