0

It might feel stupid question to ask, but I would like to understand possible way of implementations!

My application is location based. At SplashScreen of application I fetch the user location (each time) using GoogleApiClient. It also implements ResultCallback<LocationSettingsResult> and LocationListener. Everything works great here.

After this, I sign up the user with fetched location. On successful sign up, I take user to MapFragment. Now this fragment again implements GoogleApiClient, ResultCallback<LocationSettingsResult> and LocationListener.

Now my question is can I avoid this code repetitions in my both activities?

As far my knowledge, I can have a parent Activity/Fragment with all these location based Interface implementations and extend the same in SplashScreen activity and MapFragment

Is it a good practice to do so? Or is there any other better way I can do this?

Kavin Prabhu
  • 2,307
  • 2
  • 17
  • 36
  • Have a look at `Service` classes. – David Medenjak Jan 31 '16 at 17:44
  • Extending the same Activity / Fragment, wrapping logic for getting location updates is an option, but it strongly depends on your target. With activity / fragments you will have connection / disconnection from location services every time you move from a screen to another, and (if you follow guidelines) also when you turn off the display. If you need to always have a ready updated location, regardless of screen and device status, you can configure PendingIntent for receiving updates with an IntentService, store location value locally and use when needed. – andrea.petreri Jan 31 '16 at 18:12
  • For using an IntentService see here: http://stackoverflow.com/questions/30141631/send-location-updates-to-intentservice – Daniel Nugent Jan 31 '16 at 18:18
  • Obviously, `Service` can be one of the option to get the `Location` updates using `GoogleApiClient`. But because I have `ResultCallback` in my `Activity` I will have user interactions. Also I'm not sure whether I can use `BoundService` here. But using `StartService` it will be `NON_STICKEY` also I have to stop the service whenever my application is closed. I don't this service is running when app is not in foreground. I'm not sure how `BoundService` will work here! Need to give thoughts if I go with `Service` – Kavin Prabhu Jan 31 '16 at 18:20
  • @thetonrifles I have tried this, but as far I know whenever the `Activity` is not in foreground we have to disconnect `GoogleApiClient`! I have two `Activities` here so I have to disconnect from `GoogleApiClient`. Doesn't that fail to get the location updates? – Kavin Prabhu Jan 31 '16 at 18:22
  • You can use an event bus to send the updated location to your activities. https://github.com/greenrobot/EventBus. You could also use a broadcast receiver if you don't want to use a third party library. – Daniel Nugent Jan 31 '16 at 18:25
  • @DanielNugent In that link, I didn't find anything helpful. Because you have a `Activity` that implements all listeners and send the location updates to `IntentService`. But my scenario is two activities. So do I need to write the same set of implementation code in other activity too? As @DavidMedenjak said I can go with Service. It works obviously. But as I explained, in my scenario which one fits better way is what I'm trying to understand. May be I should try using `Service` once. But I'm not sure which one go better! Whether `BoundService` or `StartService`! – Kavin Prabhu Jan 31 '16 at 18:33
  • @kevin that simple example just used an Activity, but you would take that code and put it in an Application subclass. I just edited the linked answer to make it more clear, and removed the solution that had the listener in the Activity. A Service would work as well, the only difference is that a Service keeps running, and it runs on the UI thread. An IntentService only runs when it's "woken up", and it runs on a background worker thread. – Daniel Nugent Jan 31 '16 at 18:58

0 Answers0