Startting and Stopping GPS from Android App (Not from single Activity )
I have an App where I need current GPS location by one or many Activities, In an attempt to reduce the waiting time for the user, I am trying to start the GPS LocationListener at the App launch. I want when my App exits (Go to Home screen) from any of the activity, GPS listener should be stop. I am having severa开发者_高级运维l activities and any activity can use GPS but due to my App flow we are not sure from which activity user would exit So I want to maintain some global state of the GPS for Start and Stop. Basically binding the Start and Stop with App Start and Stop.
I am looking for some concrete solution for this, Any pointers would be great..Please guide!!
That doesn't sound like a good idea. You should manage GPS listeners on Activity level - attach them in onResume
and detach in onPause
. I believe the problem is that you have many Activities and you want to do the same stuff in every of them. Just create a base abstract Activity class to manage GPS connect/disconnect and extend every activity from this class.
Try to use denis.solonenko idea to handle that. If you still want to keep your own way you could try using the startActivityForResult()
function to get information when a child activity returns to your main activity.
精彩评论