I have an app working that is GPS based. It has many activities and all use the GPS. For example, one activity shows the distance to a location, another shows the speed and direction. I set up and call requestLocationUpdates() in onCreate() and call removeUpdates() in onPause().
This all works but the GPS blinks and has to re-acquire when switching activities. I did a test and if I put the removeUpdates() in onStop() instead of in onPause() there is no blinking. Apparently onStop() is called after the new activity starts which explains the lack of blinking.
I have read the posts on this subject and there seems to be a difference of opinion. However, it looks like I should use onStop() and am wondering if there is any reason I should not.
The second issue is that there is no onResume() code so there is no GPS after a back-arrow or after turning the screen off and on. I should fix this. Right now my onCreate() code creates the location manager and basically has all my code in it. It looks like following the life cycle flow chart that I should move the removeUpdates() to onStop and move the creation of the lm and ll to onStart(). Is this correct?
Here is my onCreate() code:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.set);
textView1 = (TextView)findViewById(R.id.textView1);
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
Here is my onPause code:
@Override
protected void onPause() {
super.onPause();
if(lm != null) {
lm.removeUpdates(ll);
}
ll = null;
lm = null;
}
So I think I should just change the onPause() to onStop() and make onStart() this:
@Override
public void onStart() {
super.onStart();
lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
ll = new mylocationlistener();
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, ll);
}
And my onCreate code as this;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.set);
textView1 = (TextView)findViewById(R.id.textView1);
}
This said, I am kind of new to all this and this is my first application so I would like to know what I am missing here.
I’ve done something similar to your app, except that I’ve delegated the GPS functionality to a service which stops the GPS when no activities are bound to it and starts it when an activity binds to it. I bind to the service in the onStarts and unbind in the onStops. Logging the on.. methods when I switch activities gives a sequence like this:
If done it via a service so that I’ve only got one LocationManager to control, you could either take the same approach, or requestUpdates in the onStarts() and remove them in the onStops()