soyxan
soyxan

Reputation: 91

Android rebind to foreground service after configuration change

I have an app that starts a custom foreground service that keeps running even if the app is closed.

To do that, I start the service when the user touches a button in the UI and then I bind to it in order to let the user interact with the service (start, stop, commands...) from the UI as well, for that I pass a reference to the MainActivity (where the service is created and bound) to my Composable functions.

This is my MainActivity code:

class MainActivity : ComponentActivity() {
    lateinit var mService: MainService
    private var mBound: Boolean = false

    val mViewModel = ServiceViewModel()

    private val connection = object : ServiceConnection {
        override fun onServiceConnected(className: ComponentName, service: IBinder) {
            val binder = service as MainService.LocalBinder
            mService = binder.getService()
            mBound = true
        }

        override fun onServiceDisconnected(arg0: ComponentName) {
            mBound = false
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            DormiaTheme {
                // A surface container using the 'background' color from the theme
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colorScheme.background
                ) {
            MainScreen(this) // I PASS this TO MY COMPOSABLE FUNCTIONS IN ORDER TO LET THE UI BUTTONS TO CALL THE FUNCTIONS TO INTERACT WITH THE SERVICE (START/STOP/PLAY MEDIA...)
                }
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        if (mBound) unbindService(connection)
    }
    
    fun startService(){
        val intent : Intent = Intent(this, MainService::class.java)
        startService(intent)
        if(!mBound) bindService(intent, connection, Context.BIND_AUTO_CREATE)
    }

    fun stopService(){
        if (mBound) unbindService(connection)
        stopService(Intent(this, MainService::class.java))
        mBound = false
    }

    fun playMedia(){
        if (mBound) mService.playMedia()
    }

    fun stopMedia(){
        if (mBound) mService.stopMedia()
    }

}

Everything is working fine (I can start the service form the UI and manage it), but after a configuration change (screen rotation, for instance) or if the app is in background for some time, the activity is destroyed and unbindService is called in onDestroy as expected. The thing is that I am not sure how to manage the rebinding once the Activity is resumed.

I assume I should rebind in the overriding of the onResume function, but not always, as the binding should be done only if the user has started the service before (from the UI). How to properly manage this?

Another related question is, considering that the service is already created before any binding, is it right to use Context.BIND_AUTO_CREATE when binding, or should I use another option?

Upvotes: 1

Views: 99

Answers (0)

Related Questions