Search code examples
firebasekotlinandroid-intentbundle

Can I pass a string from a activity to another one but still get it update when the original updated?


I get score from firebase in GameActivity and then pass it to ScoreActivity using bundle I want to update score in my app realtime when database changed, how can I achieve it?

Thank you.


Solution

  • According to this comment:

    The score that you get in the GameActivity is constantly changing and you want to get real-time updates about the score in ScoreActivity?

    To get real-time updates about a property that is constantly changing, you need to attach a real-time listener in the second activity as well.

    According to the official documentation:

    To read data at a path and listen for changes, use the addValueEventListener() method to add a ValueEventListener to a DatabaseReference.

    Simply passing an object through an Intent or Bundle doesn't provide this real-time feature.

    Since you didn't select a tag for a particular database, I'll also show you how to achieve this in the case of Cloud Firestore, where to listen for real-time updates you should use addSnapshotListener() on DocumentReference or on a CollectionReference object.

    So don't be afraid to use multiple listeners, all queries are pipelined over a single connection.