registerForActivityResult(下)

在registerForActivityResult(上)中我们提到使用上的不便,这篇就简单实现二次封装

赶时间看这里

maven { url "https://jitpack.io" }
api 'com.gitee.ingxin:activity-launcher:1.0'

//use
launchActivityForResult(intent) {
 //do something       
}

点击源码跳转

LaunchFragment.kt

package me.ingxin.android.activitylauncher

import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityOptionsCompat
import androidx.fragment.app.Fragment

/**
 * Created by ingxin on 2022/4/21
 */
internal class LaunchFragment : Fragment() {

    private lateinit var callback: ActivityResultCallback
    private lateinit var intent: Intent
    private var options: ActivityOptionsCompat? = null

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        outState.putInt(HOST_ACTIVITY_HASH_CODE, activity?.hashCode() ?: 0)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.d(TAG, "fragment onCreate @${hashCode()}")
        if (this::intent.isInitialized) {
            registerForActivityResult(ActivityResultContracts.StartActivityForResult()) {
                callback.onActivityResult(it)
                removeSelf()
            }.launch(intent, options)
        } else {
            val lastHasCode = savedInstanceState?.getInt(HOST_ACTIVITY_HASH_CODE) ?: 0
            val hostName = activity?.javaClass?.simpleName
            Log.e(
                TAG,
                "$hostName@$lastHasCode is destroy, current activity is $hostName@${activity?.hashCode()}"
            )
            removeSelf()
        }
    }

    fun attach(
        intent: Intent,
        options: ActivityOptionsCompat?,
        callback: ActivityResultCallback
    ) {
        this.intent = intent
        this.options = options
        this.callback = callback
    }

    private fun removeSelf() {
        parentFragmentManager.beginTransaction()
            .remove(this)
            .commitAllowingStateLoss()
    }

    companion object {
        private const val TAG = "ActivityLauncher"
        private const val HOST_ACTIVITY_HASH_CODE = "activity_hash_code"
    }

}

ActivityLauncherExt.kt

package me.ingxin.android.activitylauncher

import android.app.Activity
import android.content.Intent
import androidx.activity.result.ActivityResult
import androidx.activity.result.ActivityResultCallback
import androidx.core.app.ActivityOptionsCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity

private const val TAG = "LaunchFragment"

@JvmOverloads
fun FragmentActivity.launchActivityForResult(
    intent: Intent,
    options: ActivityOptionsCompat? = null,
    callback: ActivityResultCallback
) {
    val holder = LaunchFragment()
    holder.attach(intent, options, callback)
    supportFragmentManager.beginTransaction()
        .add(holder, TAG)
        .commitAllowingStateLoss()
}

@JvmOverloads
fun FragmentActivity.launchActivityForResult(
    clazz: Class,
    options: ActivityOptionsCompat? = null,
    callback: ActivityResultCallback
) {
    launchActivityForResult(Intent(this, clazz), options, callback)
}

@JvmOverloads
fun Fragment.launchActivityForResult(
    intent: Intent,
    options: ActivityOptionsCompat? = null,
    callback: ActivityResultCallback
) {
    val holder = LaunchFragment()
    holder.attach(intent, options, callback)
    activity?.supportFragmentManager?.beginTransaction()
        ?.add(holder, TAG)
        ?.commitAllowingStateLoss()
}

@JvmOverloads
fun Fragment.launchActivityForResult(
    clazz: Class,
    options: ActivityOptionsCompat? = null,
    callback: ActivityResultCallback
) {
    activity?.let { launchActivityForResult(Intent(it, clazz), options, callback) }
}

你可能感兴趣的:(registerForActivityResult(下))