简体   繁体   中英

service for sharing data between activities

In the android project that I have, all activities have some simple computations, but, the computations are not dependent on any activity. They depend on various sensor values. For some values of the sensors, a new activity is started. For the computations in the new activity, values from the previous activity are also required. I can pass the data in the intent, but in the transition time between starting the new activity, I miss some sensor values.

So as a workaround, I start a service that performs the computation. All the activities implement the method to start a new activity. Then the activity binds to the service and the service calls the method to start a new activity based on the sensor values. This seems to work fine and help with the data not being lost.

However, I am not sure that is this a good practice? And is there a simpler way to do this?

I like your thinking of handling the launches of each activity from a service based on sensor values. However, from the perspective of Android, this is open to severe memory leaking issues.

If I have understood correctly, you are launching an activity based on the values retained by the service, which is completely fine. However, can you think of any possibility in which you are doing a network call or any other asynchronous work in your activity which is launched? Then what about the background service launches another activity in the middle of that asynchronous call and then launches the new activity? The asynchronous call will return with some values to be provided to the calling activity which is out of context right? This will cause a memory leak and hence it may crash your application in some places.

I would like to suggest to maintain a queue for these activity switching operations in which you can handle the activity switching in more sophisticated ways. Just wait for an ongoing asynchronous call to be finished and return with some values to the calling activity and then launch the next activity. Whenever a data comes from your sensor, push the command for launching the new activity in a queue and then launch the new activity instantly if there is no current asynchronous call is running.

Hope that helps!

Update

so any guides to implement the queue suggestion?

You might consider having a basic queue implementation here. The queue will have the information of the next action to be performed and you will have another flag which will indicate if the action to be performed is safe to perform now.

if I wait for the asynchronous call to return, would I be blocking the UI?

It depends on your implementation. I am not quite sure what you are talking about as I am not very aware of the overall application design of yours.

However, I think you need to look into RxJava which enables Android to work on an event-driven mechanism.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM