Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26752] Android: Add support for `onActivityResult` callback method for activity

GitHub Issuen/a
TypeNew Feature
Priorityn/a
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsn/a
ReporterPrashant Saini
AssigneeEric Merriman
Created2018-12-14T05:25:46.000+0000
Updated2019-01-28T22:16:15.000+0000

Description

Currently we have onCreate, onPause, onResume, etc callback methods available for an activity, but we do not have any *onActivityResult* callback method due to which many things are not doable easily and straight-forward in Titanium. e.g. 1- Google Location API library allows to enable location from within the app itself without going into the device settings. But this API call returns the result in native *onActivityResult* method. 2- Some image-cropping or scanner libraries' results are returned in native *onActivityResult* method. (https://github.com/ArthurHub/Android-Image-Cropper) At present, we cannot achieve this without creating modules which is cumbersome when we already have Hyperloop. With Hyperloop we can call native *startActivityForResult* method on third-party libraries but we cannot get their results back to the activity as Hyperloop is unable to let us define *TiActivityResultHandler* interface methods nor we can cast a native Intent to Titanium intent so we can use Titanium's *startActivityForResult* method callback. It makes Hyperloop less usable if anything is to be returned in onActivityResult method. We hope to see this improvement in SDK 8.x.x itself as it will give more flexibilities in using Hyperloop.

Comments

  1. Joshua Quick 2018-12-14

    Alternatively, you can use Titanium's Ti.Android.Activity.startActivityForResult() JavaScript method. It's 2nd argument accepts a callback which will be called by Titanium's internal onActivityResult() method. https://docs.appcelerator.com/platform/latest/#!/api/Titanium.Android.Activity-method-startActivityForResult Are you able to use this approach instead?
  2. Prashant Saini 2018-12-15

  3. Rainer Schleevoigt 2019-01-20

    I have the same issue with a third party SDK. The interace calls a methd with one paramter (currentActivity) and send the result to onActivityResult... The SDK opens a new activity by Fidel.present(TiApplication.getInstance().getCurrentActivity()); https://github.com/FidelLimited/fidel-android Need support.
  4. Rainer Schleevoigt 2019-01-20

    I cannot use startActivityForResult() because the SDK only exports the method for opening the new activity, the activity self is private and I have no access. Is there a solution without patch of Titanium SDK?
  5. Hans Knöchel 2019-01-27

    Any update here? I am trying to use the [Matisse](https://github.com/zhihu/Matisse/) library which returns data via this callback. I tried hooking into the lifecycle like Ti.Facebook does [here](https://github.com/appcelerator-modules/ti.facebook/blob/master/android/src/facebook/ActivityWorkerProxy.java#L51), but it doesn't seem to be called. Unfortunately, the public void onCreate(Activity activity, Bundle savedInstanceState) signature is only available in KrollProxy instances, not KrollModule ones. So I used public void onStart(Activity activity) there, but it doesn't get called either. My code:
       @Override
       public void onStart(Activity activity)
       {
       	((TiBaseActivity) activity).addOnActivityResultListener(this);
       }
       
       @Override
       public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data)
       {
       	Log.w(LCAT, "onActivityResult called");
       }
       
    My class implements both OnActivityResultEvent and OnInstanceStateEvent. *EDIT*: Found a proper way!
  6. Rainer Schleevoigt 2019-01-27

    If onCreate is only available for proxies then we could implement the interface in a proxy. I implemented Ti.Paypal with this pattern. In my project I decompiled the aar, found the name of new activity and used it in launchActivityForResult
       @Kroll.method
       public void present() {
       	final TiActivitySupport support = (TiActivitySupport) TiApplication.getAppCurrentActivity();
       	final Intent intent = new Intent(TiApplication.getInstance().getApplicationContext(),
       			EnterCardDetailsActivity.class);
       	Fidel.FIDEL_LINK_CARD_REQUEST_CODE = support.getUniqueResultCode();
       			support.launchActivityForResult(intent, Fidel.FIDEL_LINK_CARD_REQUEST_CODE, this);
       }
       
    and
       public class TifidelModule extends KrollModule implements TiActivityResultHandler { ……… }
       
    and
       @Override
       public void onResult(Activity dummy, int requestCode, int resultCode, Intent data) {
       	if (requestCode == Fidel.FIDEL_LINK_CARD_REQUEST_CODE) {
       		if (data != null && data.hasExtra(Fidel.FIDEL_LINK_CARD_RESULT_CARD)) {
       			LinkResult card = (LinkResult) data.getParcelableExtra(Fidel.FIDEL_LINK_CARD_RESULT_CARD);
       			HashMap<String, Object> event = new HashMap<String, Object>();
       			event.put("accountId", card.accountId);
              }
          }
       }
       
       
  7. Rainer Schleevoigt 2019-01-27

    What I have understood: You try to add this listener in onStart, but it will not called.
  8. Joshua Quick 2019-01-28

    Let's not hijack [~prashant_saini]'s ticket please. It might cause confusion. This ticket is about doing this 100% in JavaScript and hyperloop, without having to write any Java code. [~hknoechel], [~titanium@webmasterei-hamburg.de], it looks like you 2 are coding in Java... in which case, there is a solution available to you now. [~titanium@webmasterei-hamburg.de], I believe I've already helped you in ticket [AC-6111]? If not, perhaps we can continue our conversation there if you don't mind. [~hknoechel], perhaps what I've written there might help you too, but it sounds like you have already solved it. (I'm recommending that you use "ti.barcode" as an example since it's simpler.)
  9. Rainer Schleevoigt 2019-01-28

    My issue is solved because I could read the class name from decompiled jar. Sorry for "hijacking" this was not my intrention.
  10. Joshua Quick 2019-01-28

    No worries. :)

JSON Source