开发者

Android : Where to call GPS : onCreate or onStart?

I'm building an android app, it basically just gets the user's coordinates using GPS, and then using those coordinates calls an API (using HTTPrequest) to get the city.

My question is, where should I be doing this (calling the GPS and the HTTPrequest) : in the onCreate() or the onStart() function of my main file?

And if I can do it 开发者_运维问答from both, which is the better practice?


It largely depends on your use case... Read again the activity lifecycle

  • I'd sugguest onStart() because the user can perfectly start the activity, do something else (android has enought resources to keep the activity in memory) and the next day, he is in another city and start the activity again, but onCreate() won't be called.
  • from how Google maps work, I'd even say that they placed the geolocalisation in onResume()
  • onStart() is always called after onCreate(). What's easy to say: it does not make sense to call it in both cases


You must call GPS only then you need to get location. Because it get many energy from your device.

The small tutorial to use GPS is here http://www.hrupin.com/2011/04/06/android-gps-using-how-to-get-current-location-example

Hope it is useful for you.


you can call your gps location class from onCreate.and then do your http request in onLocationChanged(Location location) of LocationListner.

here is the good example for gps. What is the simplest and most robust way to get the user's current location on Android?


when onstart() is called your activity is visible to the user but when onCreate() is called your activity is being Initialized.... My suggestion would be to call gps in onCreate and http request in a seperate thread.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜