jedgard
jedgard

Reputation: 868

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?

Upvotes: 0

Views: 4072

Answers (4)

Irfan Yaqub
Irfan Yaqub

Reputation: 140

If you still wanna try other solution to not run the code that you wrote in onResume for the first time when onCreate called. You can add the boolean value assign it true value in onCreate and in onResume check if it is true then skip the code else run the code that you wrote on the onRusume.

Upvotes: 0

mach
mach

Reputation: 8395

This is due to the nature of the Android activity lifecycle. Please read Pausing and Resuming an Activity for more information.

Cheers!

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006674

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.

Upvotes: 8

kosa
kosa

Reputation: 66637

That is how android activity lifecycle works.

One way to avoid this issue is, simply move logic to onResume()

Upvotes: 0

Related Questions