Closed robwatt closed 6 years ago
Hi, this plugin's onIntent() method maps to Android's onNewIntent() method in the Activity class: https://developer.android.com/reference/android/app/Activity#onNewIntent(android.content.Intent) onNewIntent() is only called for activities that set their launch mode to singleTop. I always forget the details of how it works but in the past I have used https://www.mobomo.com/2011/06/android-understanding-activity-launchmode/ as a good guide. You may have more joy using the getIntent() method of this plugin in the onResume() callback to determine the intent that launched the app.
Hi,
Ionic apps by default use 'singleTop'.
I switched onIntent() to use getIntent(), and the behaviour I see is incorrect.
When the application launches I correctly get an intent: android.intent.action.MAIN
However, when I try to share an image to the app, I expect onResume to get an intent of: android.intent.action.SEND, but I get android.intent.action.MAIN (every time).
I feel like getIntent will return the intent that launched the application, not the intent that is being used to bring the application into the foreground.
I have tried to switch my activity to singleTask, but something keeps rewriting my AndroidManifest.xml (I change it in a hook). However, I don't see how switching it to 'singleTask' would change the behvaiour. 'singleTop', should send the new Intent via onNewIntent().
I think I found the source of my issue.
It appears that when you share something from one app to the other, Cordova will call onNewIntent() BEFORE execute().
This means, that your code for the action.equals("onIntent") is called AFTER onNewIntent(). So the new intent is never sent. However, because the context is now saved, when you do it a 2nd time, it has the context so it will fire off the event to the callback.
Here is a snippet of my log showing the timeline of events.
CallbackContext is assigned AFTER onNewIntent is called
::: Tried to share a photo with my app 06-19 15:26:58.453 4518 7156 I ActivityManager: START u0 {act=android.intent.action.SEND typ=image/jpeg flg=0x10000001 cmp=io.ionic.starter/.MainActivity clip={image/jpeg U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F66/REQUIRE_ORIGINAL/NONE/956197963} (has extras)} from uid 10086
::: Cordova fires off onNewIntent() 06-19 15:26:58.637 13468 13468 D Cordova Intents Shim: On New Intent: Intent { act=android.intent.action.SEND typ=image/jpeg flg=0x10400001 cmp=io.ionic.starter/.MainActivity clip={image/jpeg U:content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F66/REQUIRE_ORIGINAL/NONE/956197963} (has extras) }
::: There is no reference to this.onNewIntentCallbackContext, so anything in the if statement is skipped 06-19 15:26:58.637 13468 13468 D Cordova Intents Shim: On New Intent: null
06-19 15:26:58.777 13468 13468 D CordovaActivity: Started the activity. 06-19 15:26:58.778 13468 13468 D CordovaActivity: Resumed the activity. ::: Cordova then fires off execute(), where the context will get cached 06-19 15:26:58.832 13468 13558 D Cordova Intents Shim: Action: onIntent 06-19 15:26:58.832 13468 13558 D Cordova Intents Shim: Callback: org.apache.cordova.CallbackContext@7154835 06-19 15:26:58.832 13468 13558 D Cordova Intents Shim: Action :: onIntent 06-19 15:26:58.832 13468 13558 D Cordova Intents Shim: Set onNewIntentCallbackContext: org.apache.cordova.CallbackContext@7154835
::: next call to onNewIntent() will work since the callbackContext has been saved
I solved this by holding onto a reference of the Intent (from onNewIntent(), if the context is null. Then in execute() if that reference exists, I would fire the event (and null out the reference afterwards).
I don't like this solution, but I am still fairly new to Cordova life cycle. This was just to test a possible solution.
Thanks, that's really interesting.
I don't have a better suggestion than what you describe i.e. maintaining a reference to the last onNewIntent received and providing that to the caller during execute(). If you're happy with it as a solution after doing some further testing can you please provide the code and I'll get it integrated with this plugin? Thanks.
Closing this issue as fix has been merged. Thanks again.
When you plan on releasing to NPM? Thanks.
Hi, will be pushing to npm next week.
Hi,
My app requires the user to be able to open it from say the Photos app (sharing images). The user can select either a single image, or multiple images.
If I:
If I repeat steps 2-4 again, I get an intent. It appears to be the correct one.
I have written a small Ionic app showing the issues
These are the intent-filters I added
` cli packages: (/usr/local/lib/node_modules)
global packages:
local packages:
`
Are you aware of this issue, or have I not integrated your plugin correctly?