Repository: expo/expo
File: packages/expo-sharing/android/src/main/java/expo/modules/sharing/SharingReactActivityLifecycleListener.kt (Kotlin)

Code before:
package expo.modules.sharing

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.facebook.react.ReactActivity
import expo.modules.core.interfaces.ReactActivityLifecycleListener
import java.lang.ref.WeakReference

class SharingReactActivityLifecycleListener(activityContext: Context) :
  ReactActivityLifecycleListener {

  private val activityContext: WeakReference<ReactActivity> =
    WeakReference(activityContext as? ReactActivity)

  override fun onCreate(activity: Activity?, misavedInstanceState: Bundle?) {
    if (activity?.intent != null && isShareIntent(activity.intent)) {
      SharingSingleton.intent = Intent(activity.intent)

      getShareIntentUri(activity)?.let { uri ->
        // Navigation handlers will ignore the intent data if the action is different than ACTION_VIEW
        // Because of this the app will always open with the default ${scheme}:/// path.
        // We change the action to `Intent.ACTION_VIEW` but store the original Intent in the singleton
        // so that the module can parse the shared data later.
        activity.intent.action = Intent.ACTION_VIEW
        activity.intent.data = uri
      }
    }
  }

  override fun onNewIntent(intent: Intent?): Boolean {
    val reactActivity = activityContext.get() ?: return false
    if (intent != null && isShareIntent(intent)) {
      emitShareIntentReceived(reactActivity, intent)
      return false
    }

    return false
  }

Review comment (suggestion):
I think we should throw here if `activityContext` is not `ReactActivity`

Code after:
package expo.modules.sharing

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Bundle
import com.facebook.react.ReactActivity
import expo.modules.core.interfaces.ReactActivityLifecycleListener
import java.lang.ref.WeakReference

class SharingReactActivityLifecycleListener(activityContext: Context) :
  ReactActivityLifecycleListener {
  private val activityContext: WeakReference<ReactActivity>

  init {
    val reactActivity = activityContext as? ReactActivity
      ?: throw IllegalArgumentException("The `activityContext` argument of SharingReactActivityLifecycleListener must be a ReactActivity")
    this.activityContext = WeakReference(reactActivity)
  }

  override fun onCreate(activity: Activity?, misavedInstanceState: Bundle?) {
    if (activity?.intent != null && isShareIntent(activity.intent)) {
      SharingSingleton.intent = Intent(activity.intent)

      getShareIntentUri(activity)?.let { uri ->
        // Navigation handlers will ignore the intent data if the action is different than ACTION_VIEW
        // Because of this the app will always open with the default ${scheme}:/// path.
        // We change the action to `Intent.ACTION_VIEW` but store the original Intent in the singleton
        // so that the module can parse the shared data later.
        activity.intent.action = Intent.ACTION_VIEW
        activity.intent.data = uri
      }
    }
  }

  override fun onNewIntent(intent: Intent?): Boolean {
    val reactActivity = activityContext.get() ?: return false
    if (intent != null && isShareIntent(intent)) {
      emitShareIntentReceived(reactActivity, intent)
      return false