Hermes Hermes - 1 year ago 85
Android Question

How to share data between multiple activities?

This will be my first project in android studio. I have a some java and C/C++ experience. I still am a total beginner though when it comes to program development. I have never developed an android app.

This app will be used to track a workout program. User will be required to enter his 1 rep maxes for each of the following; bench, squat, deadlift. I want the app to use these inputs to base the workout program on them. This will involve multiple activities sharing and manipulating this data.

What is the best way to take the user input, save it (meaning store it somewhere and left unchanged even when the app terminates), and have multiple activities access it to display? The app will only ask the user for input once. If the user wants to change input, it can be done in settings.

Answer Source

Yes, Intents are the way to pass information between activities, but as you will soon discover, they are not meant to hold whole objects of data and to keep some kind of persistence, trying to implement your data passing will become your nightmare.

Take a look for some persistence frameworks( let it be some SQLite wrappers or I personally like as they provide really cool features like live auto-updating objects, it's built upon their c++ engine and it's very easy to use )... Once you store your data in some kind of data layer, you only pass identifications for your objects through intents and query for them in the receiving activity...

That way, you keep your model persistent across the entire application, don't have the trouble to propagate changes of the object back to the source activity and lose the hell related with Parcelables ( with is a way to serialize your object to store it in intent )

Happy coding!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download