1

I am receiving some data from Broadcast receiver to Activity through listeners. Now I need that data in my composable function which I have called from Activity.

class DashboardActivity : AppCompatActivity(),BarcodeScanningReceiver.BarcodeScanningReceiverInterface {

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

    setContent {
        AppTheme {
            LoadItemRankScreen() //composable function    
        }
    }

}
override fun barcodeData(data: String) {
    // I need to pass this data param whenever it receives here
}}

Please help me to implement best approach how can I pass this data to composable function.

1
  • Use a sharedViewModel .. Commented Jul 3 at 11:04

1 Answer 1

2
class DashboardActivity : AppCompatActivity(),BarcodeScanningReceiver.BarcodeScanningReceiverInterface {
  
  private val barcodeDataState = mutableStateOf("")

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

      setContent {
        AppTheme {
          LoadItemRankScreen(barcodeDataState.value) //composable function    
        }
      }
  }
  override fun barcodeData(data: String) {
    barcodeDataState.value = data
  }
}

Here, I've created the property barcodeDataState using mutableStateOf. Then, in the overridden function barcodeData, the value of barcodeDataState will be updated. And finally this barcodeDataState is passed to the composable.

Not the answer you're looking for? Browse other questions tagged or ask your own question.