I have an android test app that handles a custom URL deep link (e.g. myapp://xxx/yyy) inside onResume as follows:
if (intent.getAction() != Intent.ACTION_VIEW) return;
String data = intent.getDataString();
if (data == null) return;
// do stuff with 'data' which is the custom URL
The problem is that this code processes the URL again and again every time the app resumes, even if it wasn't reopened using a deep link! So let's say I:
- run the app once using a custom url
- the URL gets processed correctly
- I move the app to the background again
- I reopen the app as usual (not through a deep link, just by pressing its icon)
- The URL gets processed again, as getDataString() keeps returning the last URL
- If I kill the app process and launch it regularly (not via a link) - only then getDataString() returns null and the URL processing stops.
As per some suggestions, I moved the code to onCreate(). This still runs over and over, for example, when the device rotates even if the app is still in the foreground. So onCreate is also not a good option.
How do I avoid getting the same URL over and over when the app resumes?
Based on the comments above, the solution that works best seems to be:
Code sample: