Skip to content
Receive Activity results inline, in a callback, without any boilerplate.
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Add .github folder along with issue/PR templates etc. May 23, 2019
gradle/wrapper Gradle 5.4.1 Apr 30, 2019
library Lots of unit tests Apr 25, 2019
sample Sample fix Apr 25, 2019
.gitignore Initial commit Apr 24, 2019
.travis.yml Initial commit Apr 24, 2019
LICENSE.md Initial commit Apr 24, 2019
README.md Update README.md Apr 29, 2019
RELEASE_NOTES.md 0.1.0 Apr 25, 2019
bintrayconfig.gradle Add build badge and Codacy badge Apr 25, 2019
build.gradle
dependencies.gradle Bintray plugin 0.9.1 Apr 30, 2019
gradle.properties Initial commit Apr 24, 2019
gradlew Initial commit Apr 24, 2019
gradlew.bat Initial commit Apr 24, 2019
settings.gradle Initial commit Apr 24, 2019
spotless.gradle Initial commit Apr 24, 2019
spotless.license.kt Initial commit Apr 24, 2019
versionsPlugin.gradle Initial commit Apr 24, 2019

README.md

Inline Activity Result

Download Build Status Codacy Badge License


Gradle Dependency

The library is hosted on jCenter.

dependencies {
  ...
  implementation 'com.afollestad:inline-activity-result:0.1.0'
}

What does it do?

WITH this library

You call startActivityForResult, providing an Activity to launch as the generic type. You receive the result in a callback without having to override onActivityResult. And, you don't have to worry about requestCode or resultCode.

class NewActivity : AppCompatActivity() {

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

    val extras = Bundle().apply {
      putString("some_extra", "Hello, World!")
    }
    startActivityForResult<OtherActivity>(extras) { success, data ->
      if (success) {
        toast("Got successful result!")
      }
    }
  }
}

WithOUT this library

Well, the code speaks for itself.

class OldActivity : AppCompatActivity() {

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

    val intent = Intent(this, OtherActivity::class.java)
        .putExtra("some_extra", "Hello, World!")
    startActivityForResult(intent, REQUEST_CODE)
  }

  override fun onActivityResult(
    requestCode: Int,
    resultCode: Int,
    data: Intent?
  ) {
    super.onActivityResult(requestCode, resultCode, data)
    if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
      toast("Got successful result!")
    }
  }

  companion object {
    private const val REQUEST_CODE = 69
  }
}

What's the big deal?

You do not have to override onActivityResult at all. All of your results are received inline. This may not seem like a big deal with the sample above, but it gets more valuable as you start to have more than one result case to handle. And things are scoped to the callsite, which can be very nice in various scenarios.

Note that this can all be used from within a Fragment as well.


Variants of startActivityForResult

There are multiple variants startActivityForResult you can use for different use cases. All of them allow you to pass an optional requestCode parameter, but this should generally be unnecessary.

Simple

The simplest you can get is just a generic type and the callback.

startActivityForResult<OtherActivity> { success, data ->
    // Do something
}

With Extras

You can provide a Bundle of extras to the destination Activity:

val extras = Bundle().apply {
    putString("some_extra", "Hello, World!")
}
startActivityForResult<OtherActivity>(extras) { success, data ->
    // Do something
}
    

Full Intent

And finally, you can use a full intent. In this variant you do not provide a generic parameter.

val intent = Intent(Intent.ACTION_VIEW).apply {
    setData("content://some-uri".toUri())
}
startActivityForResult(intent) { success, data ->
  // Do something
}
You can’t perform that action at this time.