- Published on
How to Reliably Detect Android Deeplinks in Expo Managed Workflow
🔗 Struggling to detect deeplinks in your Expo Managed Workflow Android app when the app is already open? Here's how to ensure your app always receives new URLs, even in the foreground.
🛑 The Problem
When you configure deeplinks for an Android app built with Expo Managed Workflow, you might notice that new URLs are not detected if the app is already running in the foreground.
🕵️ Why This Happens
If onNewIntent
is not properly implemented in your Android MainActivity
, your app won't receive new URLs from updated intents.
🛠️ The Solution
You need to update your MainActivity.kt
to call setIntent
in onNewIntent
. In Expo Managed Workflow, the best way to automate this is by creating a custom config plugin.
1. Example: Creating a Config Plugin
Create a new file at plugins/on-new-intent-plugin.js
with the following content. (Replace com.example.app
with your app's package name.)
const { withMainActivity } = require('@expo/config-plugins');
/** @type {import('@expo/config-plugins').ConfigPlugin} */
const withOnNewIntent = (config) => {
return withMainActivity(config, (modConfig) => {
const mainActivity = modConfig.modResults;
const method = `
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
setIntent(intent)
}
`;
if (!mainActivity.contents.includes('import android.content.Intent')) {
mainActivity.contents = mainActivity.contents.replace(
'package com.example.app',
`package com.example.app\nimport android.content.Intent`
);
}
if (!mainActivity.contents.includes('onNewIntent')) {
mainActivity.contents = mainActivity.contents.replace(
/class MainActivity[\s\S]*?{/,
(match) => `${match}${method}`
);
}
return modConfig;
});
};
module.exports = withOnNewIntent;
app.json
2. Add the Plugin to Add the plugin to the expo.plugins
array in your app.json
:
{
"expo": {
// ...
"plugins": [
// ...
"./plugins/on-new-intent-plugin.js"
]
}
}
3. Prebuild and Check MainActivity.kt
After adding the config plugin, run the following command to execute a prebuild:
npx expo prebuild --clean
Once prebuild is complete, open android/app/src/main/java/(your package name)/MainActivity.kt
and verify that the onNewIntent
method has been added.
Notes
- expo: 53.0.9
- react-native: 0.79.2
References: