Giter Club home page Giter Club logo

androidutilcodektx's Issues

Val cannot be reassigned

fun Context.copyToClipboard(label: String, text: String) {
val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText(label, text)
cm.primaryClip = clipData
}

最后一行报错

这个崩

java.lang.RuntimeException: Unable to start activity ComponentInfo{luyao.ktx/luyao.util.ktx.mvvm.MvvmActivity}: java.lang.IllegalStateException: You can consumeRestoredStateForKey only after super.onCreate of corresponding component
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2883)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1654)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: java.lang.IllegalStateException: You can consumeRestoredStateForKey only after super.onCreate of corresponding component
at androidx.savedstate.SavedStateRegistry.consumeRestoredStateForKey(SavedStateRegistry.java:77)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:65)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:177)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:145)
at luyao.util.ktx.base.BaseVMActivity.initVM(BaseVMActivity.kt:24)
at luyao.util.ktx.base.BaseVMActivity.onCreate(BaseVMActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2836)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1654) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6653) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) 

我用的一些扩展

1、Intent 获取参数,以 String 为例:

fun Activity.extraString(key: String, default: String = ""): Lazy<String> = lazy {
    intent?.extras?.getString(key) ?: default
}

示例:

 private val vinCode: String by extraString(DATA_VIN_CODE)

2、Boolean 的扩展,链式调用:

object Otherwise : BooleanExt<Nothing>()
class TransferData<T>(val data: T) : BooleanExt<T>()

inline fun <T> Boolean.yes(block: () -> T): BooleanExt<T> = when {
    this -> TransferData(block.invoke())
    else -> Otherwise
}

inline fun <T> BooleanExt<T>.otherwise(block: () -> T): T = when (this) {
    is Otherwise -> block.invoke()
    is TransferData -> this.data
}

示例:

isNotEmpty().yes {

}.otherwise {

}

这样嵌套用多了代码又丑又难阅读😂

3、view 防止多次点击

  • 通过时间判断
fun View.singleClick(clickEventFun: () -> Unit) {
    this.setOnClickListener {
        if (Utils.isFastClick()) {
            clickEventFun.invoke()
        }
    }
}
  • 使用 rxjava 去重(目前已废弃),
@SuppressLint("CheckResult")
@Deprecated("memory leak")
fun View.singleClick(
    intervalDuration: Long = 1,
    unit: TimeUnit = TimeUnit.SECONDS,
    eventFun: () -> Unit
) {
    Observable
        .create(object : ObservableOnSubscribe<View> {
            lateinit var mEmitter: ObservableEmitter<View>

            init {
                [email protected] {
                    mEmitter.onNext(it)
                }
            }

            override fun subscribe(emitter: ObservableEmitter<View>) {
                mEmitter = emitter
            }
        })
        .throttleLast(intervalDuration, unit)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe {
            eventFun()
        }
}

异常捕捉

mException.value = e 在案例中onError方法还是接收不到

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.