Skip to content
This repository was archived by the owner on Dec 24, 2022. It is now read-only.

仓库 files navigation

Inline 动态 Result

Android CI Codacy Badge License


Table of Contents

  1. Core
    1. Gradle Dependency
    2. Usage
  2. Coroutines
    1. Gradle Dependency
    2. Usage
  3. RxJava
    1. Gradle Dependency
    2. Usage

Core

Gradle Dependency

Core

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

Usage

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
}

Coroutines

Gradle Dependency

Coroutines

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

Usage

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...

RxJava

Gradle Dependency

RxJava

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

Usage

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()

关于

Receive 动态 results inline, without any boilerplate. Optional coroutines and RxJava support.

Topics

Resources

Stars

318 stars

关注者

0 watching

复刻s

发布

Used by

贡献者

Languages