SimpleContract

Home   »   SimpleContract

//Create custom Contract
class SimpleContract : ActivityResultContract() {

    override fun createIntent(context: Context, input: Int): Intent {
        val intent = Intent(context, SecondActivity::class.java)
        intent.putExtra("myInputKey", input)
        return intent
    }

    override fun parseResult(resultCode: Int, intent: Intent?): String? = when
    {
        resultCode != Activity.RESULT_OK -> null
        else -> intent?.getStringExtra("myInputKey")
    }
}

// Register contract
 private val pickApi = registerForActivityResult(SimpleContract()) { data ->
            Toast.makeText(this, "Data: $data", Toast.LENGTH_SHORT).show()
    }

//Call contract
  pickApi.launch(12) //12 input olarak verdi─čimiz dummy bir data

Leave a Reply

Your email address will not be published.