I want to pass a parameter between two activities at a certain time interval. The passed parameter is an int, that represents the number of satellites seen by the device and is used to draw a chart in the second activity.
4 Answers
There is no harm in keeping a GpsStatus Listener in both of those activities provided that you stop GPS properly when not needed. :)
But anyway, If I were you I'd let the Application class hold on to these details. I'd use a Service to listen to the GpsStatus and the Application class bind to this service. Your Application class is now the gateway. Your activities can now smoothly communicate with the Application data, instead of the clumsy Intents.
This pattern is taken from here
-
Be careful with this approach. If the GPS Listener is active while no Activity is active you are wasting battery life and the user will notice this through the GPS icon in the status bar. – Janusz Sep 08 '12 at 09:50
-
If the application is dead, we have code to remove the dead listeners in the Location framework. The GPS icon only appears when a request for location is made. This request can't be done from the GpsStatus class - it's only an Observer. (The GPS daemon runs when GPS is enabled. The actual battery drain happens when it starts searching for satellites. They are two different states) – Reno Sep 10 '12 at 06:42
You could use a shared preference to store the int in a kind of variable that both activities can access.
Otherwise, try a custom intent and implement a receiver in one or both Activities. This is a little harder to implement, but with the receiver you can act as soon as the value changes.
If it is rather important to do it in the time interval, use a shared preference ;-)

- 6,312
- 7
- 54
- 85
You can also pass bundles between two activities. That Intent can be used for storing
parameter values in PutIntent
.
More information here: http://remwebdevelopment.com/dev/a33/Passing-Bundles-Around-Activities.html
It is very likely that you are trying to do something in a strange way because you do not fully understood yet how activities in Android are working.
There is only one active Activity in your whole app. Sending data from one activity to another is only needed if the users changes the active Activity, either through clicking on a button and switching to a new Activity or through clicking back and going back to the previous Activity. Please read the Activity Lifecycle very carefully to understand how activities should be used.
I assume that you have registered a location listener in one activity and try to use the values of this listener in other activities. Normally you would use the onPause method to deactivate the listener in the first activity and then activate a new one in the new activity. This will get you the number of known satellites in every activity. If the user goes back you will have to reregister the listener in the onResume method to reenable the gps updates for the new Activity.
Keeping an active GPS Listener in a paused Activity will lead to an active GPS Sensor even if your App is in the background and not used anymore. This will drain the battery of the phone very fast and very likely have your app uninstalled in an instance.

- 187,060
- 113
- 301
- 369