Best approach for creating a reusable object to get users location?

I have an app which currently consists of 2 activities. Inside the first activity there is a Fragment which itself contains a MapFragment. The other activity contains a ViewPager which holds two fragments, one of which is also a Fragment that itself holds a MapFragment. In both fragments that contain the MapFragment I need to request the users current location (only once, when the user requests it). To get the users location I’m using Google Play Services Location API. What would be the best approach here for avoiding duplicate code in two of the mentioned fragments containing a MapFragment (managing connection errors etc)? Should I create an IntentService or try to manage with a plain java class or are there better ways that I’m currently not aware of?

Answer

I’d make a custom Application and call getLastLocation when requested by user – then store that location in a field and make a getter for that field.