1

I wanted to pass a MediaStream object within a Fragment to an Activity using the Navigation Component. The MediaStream class belongs to WebRTC. So, I could not touch it to make it parcelable or serializable for passing the object around.

Here is the code from the fragment:

// Creating a PeerConnection with two callbacks 
// one is triggered when an ICE candidate is received
// the other one is triggered when a MediaStream is received
localPeer = peerConnectionFactory.createPeerConnection(
            rtcConfig,
            object: CustomPeerConnectionObserver("localPeerCreation"){
                override fun onIceCandidate(iceCandidate: IceCandidate?) {
                   // not relevant for this talk 
                }

                override fun onAddStream(mediaStream: MediaStream?) {
                    super.onAddStream(mediaStream)
                    // TODO: create a new Activity and pass media stream to it for displaying
                    StreamsFragmentDirections.actionStreamsFragmentToStreamActivity(mediaStream)
                }
            })!!

As the TODO line implies, my initial goal was to pass the MediaStream object to a new Activity which should display the stream. But I did not know how to do it. In the Navigation Graph Editor, I selected <inferred type> as type for mediastream but that not worked out as expected. Here, the relevant part from the navigation graph xml layout:

<activity
        android:id="@+id/streamActivity"
        android:name="com.john.elia.ui.activities.StreamActivity"
        android:label="StreamActivity" >
        <argument 
            android:name="mediaStream" />
</activity>

But the compiler complains, saying that it expected an Int but found a MediaStream. How can I pass the MediaStream object ? In all the examples I have found they only show how to pass primitive types like a String, Int etc. How about objects with custom types?

2
  • You can only send Parcelable or Serializable objects to Activities, that true with or without Navigation. Commented Jan 17, 2020 at 18:21
  • As per docs supported arguments for custom size should be parcelable or serializable. Commented Jan 17, 2020 at 18:24

1 Answer 1

2

You may not be able to make every type of object implement Parcelable, but by following the principles of modern app architecture, you don't have to do so any more, see also the Guide to app architecture

The general idea is to have your data in a repository (the single source of truth) which can be accessed by Activitys and Fragments via some custom class extending ViewModel.

So once you obtain any type of data (the MediaStream) in the Fragment, you should pass it to the repository, navigate to the next UI component and have it fetch the data from the repository with the help of its own ViewModel.

Sign up to request clarification or add additional context in comments.

5 Comments

Aah, ok. ViewModel...the other Android Arch. Component. I think I got the idea. Thank you.
Note that you can't share a ViewModel across activities, so that'd only be useful if you were sharing something across different Fragments, etc. (i.e., within a single activity).
@ianhanniballake - but I can access the same repository from different ViewModels so if I let the repository keep the data this approach will work
although I am sure that this approach might worked, I did not want to add an extra ViewModel just to pass an object back and forth. What I did was to put the whole WebRTC stream-related logic into an already existing Fragment so that everything is in one place. No need to pass sth. around anymore.
@aminakoy, that's OK if it works for you. In some cases people noticed that the Fragment was destroyed (e.g. because of an orientation change of the device) and so the whole data fetching operation had to be started all over. That's why ViewModel was introduced: it does survive an orientation change so when the Fragment is recreated it can reconnect to the same ViewModel instance (and so indirectly to the repository as well). This way, data which has been succcessfully loaded will not be lost. But, again: if your approach works for you, everything is fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.