Somk
Somk

Reputation: 12047

Android same method called twice

I have a method that runs when the app is opened. It is called from onCreate. But I also call it from onResume. The problem I have is that at the moment it is running twice when the app opens. Is there a way to stop this or a better way to implement what I am trying to achieve?

Thanks

Upvotes: 4

Views: 3548

Answers (3)

ArcDare
ArcDare

Reputation: 3106

Activity lifecycle needed steps:

Want to Start the Activity?

Activity Starts => onCreate() => onStart() => onResume() => Activity is running

Want to shut it down?

Running => onPause() => onStop() =>onDestroy() => Activity is finally shut down.

I recomend you to read this article of the official documentation: Activity

Upvotes: 0

Gangnus
Gangnus

Reputation: 24464

Why not to use it only in onResume()? It IS called after onCreate anyway.

viz. http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Upvotes: 0

Yashwanth Kumar
Yashwanth Kumar

Reputation: 29121

When an activity starts, onCreate() is called first and then onResume() is called,if you want it to be called only once, remove the call in onCreate().

calling the method in onResume() will ensure that the method is called when the activity regains focus, like after back press etc..

Upvotes: 13

Related Questions