Pranit Harekar
Pranit Harekar

Reputation: 433

Pass React Native Promise between activities on Android [Kotlin]

FYI: I am new to Android world.

What I am trying to do ?

I am using Native Modules for React Native Android. I am using react native promises instead of callbacks to do some async task. Simple and Easy.

In Detail

I have a native module called "ReactNativeBridge" which has bunch of methods which are marked with @ReactMethod so they can be accessed from React Native side.

class ReactNativeBridge(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
@ReactMethod
printImage(imageUrl: String, promise: Promise) {
    val currentActivity = currentActivity

     if (currentActivity == null) {
        promise.reject("Activity doesn't exist")
    }

    // Start activity which takes promise as an argument
     val intent = Intent(reactApplicationContext.applicationContext, PrintActivity::class.java).apply {
        putExtra(EXTRA_PRINT_DATA, somePrintData)
        putExtra(EXTRA_PRINT_PROMISE, promise)   // <---- here is the problem
    }

    currentActivity.startActivity(intent)
} }

As you can see I want to start a new Android activity in each of these methods and pass the Promise as an argument to those activities, through Intents. But I am unable to pass the promise through Intents since not all data types can be passed through Intents. I checked the type of promise, it is an Interface.

This is how a Promise Interface looks,

enter image description here

I know we cannot pass Interface since it is an abstract thing (like a blueprint).

** If your answer is "extend interface to Serializable" then remember I cannot modify Promise Interface. (It's a Facebook thing)

Questions

  1. How to pass a Promise type through Intent ?
  2. Is there any other approach to pass the Promise? Other than Intents.

Upvotes: 4

Views: 1878

Answers (1)

unify
unify

Reputation: 6351

You should consider using startActivityForResult. and resolve your promise when you collect the result you expect from the second Activity

Upvotes: 0

Related Questions