dependencies {
...
implementation 'com.afollestad.inline-activity-result:core:0.2.0'
}You call start动态ForResult, providing the 动态 to launch as the generic type. You
receive the result in a callback without having to override on动态Result. And, you don't
have to worry about requestCode or resultCode.
class 新建动态 : AppCompat动态() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val extras = Bundle()
.putString("some_extra", "Hello, World!")
start动态ForResult<Other动态>(extras) { success, data ->
if (success) {
toast("Got successful result: $data")
}
}
}
}There are multiple variants start动态ForResult you can use for different use cases. All of
them allow you to pass an optional requestCode parameter, but this should generally be unnecessary.
First, the simplest you can get is just a generic type and the callback.
start动态ForResult<Other动态> { success, data ->
// Do something
}Second, you can provide a Bundle of extras to the destination 动态:
val extras = Bundle()
.putString("some_extra", "Hello, World!")
start动态ForResult<Other动态>(extras) { success, data ->
// Do something
}
And finally, you can use a full intent. In this variant you do not provide a generic parameter.
val intent = Intent(Intent.ACTION_VIEW)
.setData("content://some-uri".toUri())
start动态ForResult(intent) { success, data ->
// Do something
}dependencies {
...
implementation 'com.afollestad.inline-activity-result:coroutines:0.2.0'
}You can use Kotlin coroutines to get rid of the callback. It of course is a suspend function so it must be called within a coroutine scope.
Instead of start动态ForResult, you can use start动态AwaitResult:
val result: 动态Result = start动态AwaitResult<Other动态>()
// use result...dependencies {
...
implementation 'com.afollestad.inline-activity-result:rxjava:0.2.0'
}You can use RxJava to integrate the 动态 launch and result into your streams.
Instead of start动态ForResult, you can use start动态EmitResult:
val disposable = start动态EmitResult<Other动态>()
.subscribe { result ->
// use result...
}
// make sure you dispose of the subscription when your 动态/Fragment goes away
disposable.dispose()