Skip to content

Instantly share code, notes, and snippets.

@mochadwi
Last active March 20, 2020 18:56
Show Gist options
  • Save mochadwi/032bbc39e6341ecff1deb3a2545e7c0a to your computer and use it in GitHub Desktop.
Save mochadwi/032bbc39e6341ecff1deb3a2545e7c0a to your computer and use it in GitHub Desktop.
Passing extras with intent while using intent with action ACTION_GET_CONTENT

Original answer from StackOverflow:

  1. setResult(RESULT_OK, yourIntentWithExtras), click here for more details

Usage:

01. MainActivity.kt
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.putExtra("viewId", myViewIdParam)

intent.setType("image/*")

context.setIntent(intent)
context.startActivityForResult(intent, REQUEST_IMAGE_PICK)
02. DetailActivity.kt
val resultIntent = intent

resultIntent.putExtra("viewId", intent.getIntExtra("viewId", -1));

setResult(RESULT_OK, resultIntent);
finish();
03. MainActivity.kt (Receive your data to check activity intent and data intent
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == Activity.RESULT_OK && requestCode == REQUEST_IMAGE_PICK && data != null) {
            val viewId = data.getIntExtra("viewId", intent.getIntExtra("viewId", -1))
            // todo your stuff here
        }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment