Titanium JIRA Archive
Appcelerator Community (AC)

[AC-6111] onActivityResult method for KrollModule

GitHub Issuen/a
TypeNew Feature
Resolution Date2019-01-24T20:14:48.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
ReporterRainer Schleevoigt
AssigneeShak Hossain


Some Third party SDK (i.e. paypal) opens a new activity and sends the result to onActivityResult. For this we can use this pattern:
activitySupport.launchActivityForResult(intent,REQUEST_CODE, new ResultHandler());
The intent contains the new activity. Some other SDKs exposes only the method for opening the new (private) activity (i.e. fidel.uk). In this case I have no access to the activity and I cannot use the pattern above. I can only use the open method and the result (aspected in onActivityResult) I cannot access. Krollmodule class expose lifecycle ecents (onPause etc,), but not onActivityResult. Hoe I can resolve this problem?


  1. Rainer Schleevoigt 2019-01-22

    Now I have decompiled the SDK and now I know the name of new activity class and I can use:
       public void present() {
       	TiActivitySupport support = (TiActivitySupport) TiApplication.getAppCurrentActivity();
       	Fidel.FIDEL_LINK_CARD_REQUEST_CODE = support.getUniqueResultCode();
       			new Intent(TiApplication.getInstance().getApplicationContext(), EnterCardDetailsActivity.class),
       			Fidel.FIDEL_LINK_CARD_REQUEST_CODE, new PaymentResultHandler());
    Ok in this project I could solve, but in common cases, how can I receive onActivityResult?
  2. Joshua Quick 2019-01-23

    Have a look at our "ti.barcode" module as an example here... https://github.com/appcelerator-modules/ti.barcode/blob/master/android/src/ti/barcode/BarcodeModule.java Particularly the code here... https://github.com/appcelerator-modules/ti.barcode/blob/master/android/src/ti/barcode/BarcodeModule.java#L321 You'll first to acquire a unique result code from the TiActivitySupport Java class via its getUniqueResultCode() method. You should never hard code the result code on the library side to avoid collision with other libraries. This is standard practice on Android (it's up to the activity implementor to set the unique result codes, not the library). The 3rd argument you provide to the TiActivitySupport.launchActivityForResult() method is an instance of TiActivityResultHandler. You need to implement its onResult() and onError() methods on your end because they'll be called when a result has been received or if it failed to launch the child activity. https://docs.appcelerator.com/module-apidoc/latest/android/org/appcelerator/titanium/util/TiActivityResultHandler.html The above mentioned "ti.barcode" module's BarcodeModule class implements the TiActivityResultHandler itself. You can do it this way too or implement your own handler.
  3. Rainer Schleevoigt 2019-01-23

    Thanks for your quick answer. But in my use case I cannot start intent because the new activity is not documented. The only documented interface is a method show(fooactivity). Now I have decompiled the SDK and I see the name of the activity and it is public. So I can use the common pattern. Unfortunately the result event will never fired and after closing the second activity ANR happens, if I don't start the module in the root activity (i.e.) after window opening. Here the code: https://github.com/AppWerft/Ti.Fidel/blob/master/android/src/ti/fidel/TifidelModule.java#L124-L143 Do you have an idea?
  4. Joshua Quick 2019-01-23

    Are you saying this 3rd party activity can't be shown via the native [startActivityForResult()](https://developer.android.com/reference/android/app/Activity.html#startActivityForResult(android.content.Intent,%20int)) method? Let's forget about Titanium for the moment. In order to receive the activity's result natively, you need to feed the above mentioned method a requestCode, which is a unique integer ID that is set by the calling activity. Doing so will cause that same activity to have its onActivityResult() method called with the given request code. That's how it's supposed to be done natively. So, does the 3rd party library not allow you to do this? What is this show(activity) method that are you speaking of? It sounds like this 3rd party library might be doing something unusual. If so, then they would need to provide a code example on how to do this. If you can provide a link to this library's docs, then we might be able to help out.
  5. Rainer Schleevoigt 2019-01-24

    The 3party SDK realise a billing interface for creditcards.Therefore there is only a method present() and the result will send to onActivityResult. This is the only interface. Now I have decompiled the aar (I know this is not permitted)and now I know the name of the activity class. Good news: it works:
       public void present() {
       	_instance = this;
       	final TiActivitySupport support = (TiActivitySupport) TiApplication.getAppCurrentActivity();
       	final Intent intent = new Intent(TiApplication.getInstance().getApplicationContext(),
       	Fidel.FIDEL_LINK_CARD_REQUEST_CODE = support.getUniqueResultCode();
       	if (TiApplication.isUIThread()) {
       		support.launchActivityForResult(intent, Fidel.FIDEL_LINK_CARD_REQUEST_CODE, this);
       	} else {
       		TiMessenger.postOnMain(new Runnable() {
       			public void run() {
       				support.launchActivityForResult(intent, Fidel.FIDEL_LINK_CARD_REQUEST_CODE, _instance);
    and the handling of result here: https://gist.github.com/AppWerft/d6f7571b04b28b62c1168dd85fc96f8d Many thanks for you help,you comment solves my issue!
  6. Joshua Quick 2019-01-24

    Glad you got it working! Happy to help. :)

JSON Source