This is so simple but still doesn't work. I simply want to pass a string to the ACTION_PICK Intent to retrieve it in registerForActivityResult.
Here is my code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val gallery = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI)
gallery.putExtra("filename", "test")
resultLauncher.launch(gallery)
}
private var resultLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == Activity.RESULT_OK) {
val fileName: String = result.data?.getStringExtra("filename").toString()
Log.i("###", "Filename is $fileName")
}
}
}
First I'm creating the Intent and call .putExtra to pass a String, in registerForActivityResult I call result.data?.getStringExtra("filename").toString() but it prints out "Filename is Null".
What am I doing wrong?
That is not supported, sorry. The
Intentthat you use to start the activity is not the sameIntentthat you receive in response. And, there is no requirement for extras on the firstIntentto get copied into the secondIntent.