2

In my application, There is one activity where a publisher and three subscribers can have a conversation among each other in a single session. I have developed the whole concept in portrait mode. Now the requirement is when user rotate his device from portrait mode to landscape mode the current view what I am showing in portrait mode will be changed and new view will be shown to the user.

Problem 1. Are these things feasible with Open Tok (Can Session, publisher and subscriber be maintained on orientation changes)?

Problem 2. I don't want to recreate the session with publisher and subscriber. How can I retain the current session, created publisher and subscriber on Activity orientation changes?

Problem 3. As Open Tok provides an Android View for publisher and subscriber is it a good idea to hold them in memory on orientation changes.

Problem 4. When I try this thing with the Fragment where I set "setRetainInstance(true);" After rotating the screen first time it worked fine for me but the second time it gave the following exception :

com.getvokl.android.GetVoklAPP E/ActivityThread: Activity com.getvokl.android.GetVoklAPP.framework.activity.CallFriendActivity has leaked IntentReceiver com.opentok.android.Session$20@d210317 that was originally registered here. Are you missing a call to unregisterReceiver()?

When I checked It occurred on onResume() method of my Fragment class where I mentioned session.onResume().

Vinit Saxena
  • 535
  • 1
  • 9
  • 24

2 Answers2

0

There are several options to save the Opentok objects in the Activity restart cycle.

One option can be saving instances in an object whose lifecycle is different than the Activity. You could use a static instance inside the Activity too. Take a look to this Kotlin sample using companion objects:

class OpenTokState {
    lateinit var s: Session
    lateinit var p: Publisher
    var initialized = false
}

class MainActivity : AppCompatActivity() {
    val API_KEY = ".."
    val TOKEN = "..."
    val SESSION_ID = "..."


    companion object {
        var opentok = OpenTokState()
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        ...
        if (!opentok.initialized) {
            opentok.initialized = true
            opentok.s = Session.Builder(this, API_KEY, SESSION_ID).build()
        ...
    }
}

The companion object is a static instance that, as static means, will be the same in all MainActivity instances.

Please note that if you follow this approach, you need to take one important consideration, you need to remove the publisher/subscriber view from its parents in the onDestroy method of the activity and add their views again to the new instances of the containers.

There is another alternative, but as Android documentation states, it is not the prefered way. This another method is by handling the configuration changes by yourself. In this case, you can add android:configChanges="orientation" to your activity declaration in the app manifest, and then the activity will not be restarted when changing orientation, so the instances will remain alive.

However, you would need to handle the rest of the changes that happens (like layout changes) by yourself, which will be probably not an easy task.

HyLian
  • 4,643
  • 3
  • 31
  • 37
0

The easiest way for screen rotation handling is to implement ViewModel from Architecture Components.

kostyabakay
  • 1,408
  • 2
  • 16
  • 28