Titanium JIRA Archive
Appcelerator Community (AC)

[AC-6202] Android: TiActivitySupport support startResolutionForResult

GitHub Issuen/a
TypeImprovement
Priorityn/a
StatusClosed
ResolutionNeeds more info
Resolution Date2019-06-10T23:45:24.000+0000
Affected Version/sn/a
Fix Version/sn/a
Componentsn/a
Labelsn/a
ReporterAhmed Mohamed
AssigneeJoshua Quick
Created2019-04-07T16:46:47.000+0000
Updated2021-02-02T14:08:00.000+0000

Description

In my com.locationservices module I can't get onActivityResult by using TiActivitySupport because it doesn't support startResolutionForResult void from ResolvableApiException https://github.com/AhmedMSayed/TiLocationServices/blob/master/android/src/com/locationservices/LocationservicesModule.java

Comments

  1. Sharif AbuDarda 2019-04-09

    Hello [~ahmed.mohamed20320], Can you share some native guide on how this is handled in there? Thanks.
  2. Ahmed Mohamed 2019-04-10

    ResolvableApiException provides a method to resolves an error by starting any intents requiring user interaction. [See Description here](https://developers.google.com/android/reference/com/google/android/gms/common/api/ResolvableApiException) startResolutionForResult is a method like startActivityForResult method which used to start an activity and wait for a result that will be catched later in onActivityResult method. At now Titanium support startActivityForResult by using launchActivityForResult method in TiActivitySupport and catched the results by onResult So we need to support startResolutionForResult to get its results too in onResult You can test it on my module [here](https://github.com/AhmedMSayed/TiLocationServices/blob/5a46495ee567d6ca91a8cc727a21dbd0f5146651/android/src/com/locationservices/LocationservicesModule.java#L137)
  3. Joshua Quick 2019-04-10

    This is possible to do now. Instead of using the TiActivitySupportHelper.launchActivityForResult() method, you'll want to create your own version of that code. You'll need to create your own TiActivityResultHandler object, pass it into the TiActivitySupportHelper.registerResultHandler() method (which Titanium's activity queries in its onActivityResult() method, and then call the startResolutionForResult() with the request code and activity you are launching it from. I can write up a code example tomorrow.
  4. Joshua Quick 2019-04-10

    In your code, get rid of this part...
       resolvable.startResolutionForResult(TiApplication.getAppRootOrCurrentActivity(), REQUEST_CHECK_SETTINGS);
       
    And do this instead...
       // Add these imports to the top...
       #import org.appcelerator.titanium.TiBaseActivity;
       #import org.appcelerator.titanium.TiLifecycle;
       
       // Replace your startResolutionForResult() line with the below...
       org.appcelerator.titanium.TiBaseActivity activity = TiApplication.getAppRootOrCurrentActivity();
       if (activity != null) {
       	final int MY_REQUEST_CODE = activity.getUniqueResultCode();
       	final resultListener = new org.appcelerator.titanium.TiLifecycle.OnActivityResultEvent() {
       		@Override
       		public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
       			// This listener is invoked for all request codes. So, ignore the ones not directed to us.
       			if (requestCode != MY_REQUEST_CODE) {
       				return;
       			}
       
       			// This is our request code.
       			// Handle the result code returned by startResolutionForResult() here.
       		}
       	};
       	activity.addOnActivityResultListener(resultListener);  // <- Stores listener as weak reference.
       	resolvable.startResolutionForResult(activity, MY_REQUEST_CODE);
       }
       
    Unfortunately our Titanium activity's addOnActivityResultListener() method stores the listener as a weak reference. So, you need to hold on to the listener as a strong reference temporarily via final. I'm doing it by boxing it above. Alternatively, you can implement the OnActivityResultEvent via your LocationservicesModule class but then you have to change the request code handling. I hope this helps!
  5. Sharif AbuDarda 2019-05-09

    Hello [~ahmed.mohamed20320], Can you follow up here? what's the status of your issue? did you try Han's guide?
  6. Ahmed Mohamed 2021-02-02

    Thanks @Joshua Quick it works like a charm. https://github.com/AhmedMSayed/ti.locationservices/blob/7d65d126e43c1bf600530ee4a0c841c850fe4388/android/src/ti/locationservices/TiLocationservicesModule.java#L120

JSON Source