How/Can I override onActivityResult ?

Hi,

I’m testing to implement UMeng social media integration in my app. One of the requirement for SSO is that I need to override the onActivityResult method.

I assume there’s only one Activity in Corona app which can be accesible from getActivity and I can see that it already has onActivityResult implemented.

Can I override it? 

I’m following the samples app and put my code as plugin…

Thanks

Hi you can overload the  com.ansca.corona.CoronaActivity  class but beware that some features,

like push notification for exemple, might not work. An don’t forget to call super methods ;) 

Not also that you will have to change your AndroidManifest accordingly. 

Good luck ! 

This method might be of interest to you.  This will give you access to the callback.

http://docs.coronalabs.com/native/android/html/com/ansca/corona/CoronaActivity.html#registerActivityResultHandler(com.ansca.corona.CoronaActivity.OnActivityResultHandler)

I have an example on how to use our registerActivityResultHandler() method here…

   http://forums.coronalabs.com/topic/37282-how-can-one-capture-cameragallery-return-result-in-android-native-code/?p=193386

Joshua Quick

I need to override onActivityResult of CoronaActivity.

Your following answer has a limitation.

http://forums.coronalabs.com/topic/37282-how-can-one-capture-cameragallery-return-result-in-android-native-code/?p=193386

Using CoronaActivity.OnActivityResultHandler only works when you can call directly activity.startActivityForResult with the requestCode which is returned by CoronaActivity.registerActivityResultHandler().

But I need to use a library in which activity.startActivityForResult is called with a constant value of requestCode I cannot specify.

So, made MyCoronaActivity which extends CoronaActivity and override onActivityResult().

And I changed AndroidManifest.xml like this.

        <!-- The main Corona activity that runs the Corona project. -->

        <activity android:name=“com.mycompany.myproject.MyCoronaActivity”

                  android:screenOrientation=“portrait”

                  android:configChanges=“keyboardHidden|orientation|screenSize”

                  android:label=“MyProject”

                  android:launchMode=“singleTask”

                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

            <intent-filter>

                <action android:name=“android.intent.action.MAIN” />

                <category android:name=“android.intent.category.LAUNCHER” />

            </intent-filter>

        </activity>

It worked.

But I’m worried about this solution.

Is there a no problem with this solution?

I know that it breaks at least the push notifications (as they starts CoronaActivity.class directly). 

I Didn’t notice any other issues for now … 

Are you saying that the 3rd party library that you are using has a hard coded request code?  If so, then that is bad design on their end because they could run into the issue where another library might want to use the same request code that they’re using, causing collision.  I’d recommend that you ask them to change it if possible, because no other 3rd party Android library that I’ve seen does this for the reason I’ve mentioned above.

But yeah, you have no choice but to derive from our CoronaActivity in this case.  And Darune is right.  Tapping local or push notifications will not display your CoronaActivity derived activity because our notification code is hardcoded for the CoronaActivity class.  In fact, there might be other issues that I may not be aware of as well.  You’ll have to test every feature in your app to ensure it works as expected.

Also, the limitation that we imposed where we do not allow the developer to set the request code is intentional.  The reason is because there is no guarantee that we can reserve the given request code in case another library/plugin reserved it first.  This is especially the case for 3rd party plugins.  In fact, notice that our registerActivityResultHandler() never allows a 3rd party Corona plugin to listen in to another plugin’s OnActivityResultHandler().  This too was intentional to secure the communications and to help prevent potential plugin bugs where a developer might accidentally handle the result belonging to another plugin.  A lot of thought went into the design.  I’d prefer to keep it this way.

In my case the registerActivityResultHandler would have done the job.

But I was not aware of this method at that time. I agree that subclassing CoronaActivity is not the best idea as it involve knowing the code you don’t have access to… 

Good luck 

Hi you can overload the  com.ansca.corona.CoronaActivity  class but beware that some features,

like push notification for exemple, might not work. An don’t forget to call super methods ;) 

Not also that you will have to change your AndroidManifest accordingly. 

Good luck ! 

This method might be of interest to you.  This will give you access to the callback.

http://docs.coronalabs.com/native/android/html/com/ansca/corona/CoronaActivity.html#registerActivityResultHandler(com.ansca.corona.CoronaActivity.OnActivityResultHandler)

I have an example on how to use our registerActivityResultHandler() method here…

   http://forums.coronalabs.com/topic/37282-how-can-one-capture-cameragallery-return-result-in-android-native-code/?p=193386

Joshua Quick

I need to override onActivityResult of CoronaActivity.

Your following answer has a limitation.

http://forums.coronalabs.com/topic/37282-how-can-one-capture-cameragallery-return-result-in-android-native-code/?p=193386

Using CoronaActivity.OnActivityResultHandler only works when you can call directly activity.startActivityForResult with the requestCode which is returned by CoronaActivity.registerActivityResultHandler().

But I need to use a library in which activity.startActivityForResult is called with a constant value of requestCode I cannot specify.

So, made MyCoronaActivity which extends CoronaActivity and override onActivityResult().

And I changed AndroidManifest.xml like this.

        <!-- The main Corona activity that runs the Corona project. -->

        <activity android:name=“com.mycompany.myproject.MyCoronaActivity”

                  android:screenOrientation=“portrait”

                  android:configChanges=“keyboardHidden|orientation|screenSize”

                  android:label=“MyProject”

                  android:launchMode=“singleTask”

                  android:theme="@android:style/Theme.NoTitleBar.Fullscreen">

            <intent-filter>

                <action android:name=“android.intent.action.MAIN” />

                <category android:name=“android.intent.category.LAUNCHER” />

            </intent-filter>

        </activity>

It worked.

But I’m worried about this solution.

Is there a no problem with this solution?

I know that it breaks at least the push notifications (as they starts CoronaActivity.class directly). 

I Didn’t notice any other issues for now … 

Are you saying that the 3rd party library that you are using has a hard coded request code?  If so, then that is bad design on their end because they could run into the issue where another library might want to use the same request code that they’re using, causing collision.  I’d recommend that you ask them to change it if possible, because no other 3rd party Android library that I’ve seen does this for the reason I’ve mentioned above.

But yeah, you have no choice but to derive from our CoronaActivity in this case.  And Darune is right.  Tapping local or push notifications will not display your CoronaActivity derived activity because our notification code is hardcoded for the CoronaActivity class.  In fact, there might be other issues that I may not be aware of as well.  You’ll have to test every feature in your app to ensure it works as expected.

Also, the limitation that we imposed where we do not allow the developer to set the request code is intentional.  The reason is because there is no guarantee that we can reserve the given request code in case another library/plugin reserved it first.  This is especially the case for 3rd party plugins.  In fact, notice that our registerActivityResultHandler() never allows a 3rd party Corona plugin to listen in to another plugin’s OnActivityResultHandler().  This too was intentional to secure the communications and to help prevent potential plugin bugs where a developer might accidentally handle the result belonging to another plugin.  A lot of thought went into the design.  I’d prefer to keep it this way.

In my case the registerActivityResultHandler would have done the job.

But I was not aware of this method at that time. I agree that subclassing CoronaActivity is not the best idea as it involve knowing the code you don’t have access to… 

Good luck 

Well, it seems there’s no way around it since looks like I need to subclass CoronaActivity. The 3rd party SDK requires me to initiate everything when the activity is created (onCreate) and override some callbacks.
Unless there’s a way to get around this, I like to hear it.

Darune, you mentioned some features won’t work. Can you tell me what are they according to your experience? For now, push notification isn’t what I desperately need so that’s okay.

As fas as I am concern I didn’t notice any other issue, but that doesn’t mean that there are none… 

My game has been one year in production and no crash log reported 

Good luck 

Yanuar, are you sure that you need to initialize the 3rd party library in the onCreate() method?

Because in my experience, it’s not required.  I know that most 3rd party libraries’ sample code put their initialize code their, but that’s merely out of convenience.  You can defer the initialize to just a bit later, such as via the CoronaRuntimeLister.onLoaded() callback which gets called just before your “main.lua” gets executed…

   http://docs.coronalabs.com/native/android/html/com/ansca/corona/CoronaRuntimeListener.html

Have a look at the “SimpleLuaExtension” sample project included with Corona Enterprise.  It shows you how to listen for Corona runtime events.  It’s a much easier approach compared to deriving from the CoronaActivity class.  Plus, you may run into some limitations with deriving from the CoronaActivity class.  The biggest one that I can think of is that tapping push notifications on the status bar is hard coded to launch the CoronaActivity class and not your derived version.  There might be other issues that I’m not aware of either, but that’s the biggest gotcha that I can think of.

Hi Joshua,

I discussed this with the 3rd party support and yes I aware that I can initiate it somewhere else. The problem is : it still require me to override the onPause and onResume to add their specific code on those events.So I really not sure how I can do that without overriding the CoronaActivity.

 

The CoronaRuntimeLister class’ onSuspended() and onResumed() get called when the activity is paused and resumed.  So, you can use those.  Just be aware that they’re called on Corona’s runtime thread and not the main UI thread.

If you look at our “Ad” app template for Android, you can see that we override those above methods as well.  This is where the ad plugin provider would stop/resume fetching ads from their servers.