-
Notifications
You must be signed in to change notification settings - Fork 3.5k
[url_launcher] Add Android support for externalNonBrowserApplication #9993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -69,6 +69,17 @@ class _MyHomePageState extends State<MyHomePage> { | |
} | ||
} | ||
|
||
Future<void> _launchInNonBrowserExternalApp(String url) async { | ||
if (!await launcher.launchUrl( | ||
url, | ||
const LaunchOptions( | ||
mode: PreferredLaunchMode.externalNonBrowserApplication, | ||
), | ||
)) { | ||
throw Exception('Could not launch $url'); | ||
} | ||
} | ||
|
||
Future<void> _launchInCustomTab(String url) async { | ||
if (!await launcher.launchUrl( | ||
url, | ||
|
@@ -187,6 +198,14 @@ class _MyHomePageState extends State<MyHomePage> { | |
: null, | ||
child: const Text('Launch in browser'), | ||
), | ||
ElevatedButton( | ||
onPressed: _hasCustomTabSupport | ||
? () => setState(() { | ||
_launched = _launchInNonBrowserExternalApp(toLaunch); | ||
}) | ||
: null, | ||
|
||
child: const Text('Launch in non-browser app'), | ||
), | ||
const Padding(padding: EdgeInsets.all(16.0)), | ||
ElevatedButton( | ||
onPressed: () => setState(() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
FLAG_ACTIVITY_REQUIRE_NON_BROWSER
flag was introduced in API level 30. While the system may ignore unknown flags on older versions, it's best practice to explicitly check the device's API level before adding a flag that is not available on all supported versions. This makes the code more robust and self-documenting about its API level dependency. You may need to add an import forandroid.os.Build
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gmackall @reidbaker Does the Android team have a preference here? I had considered adding this but then didn't because presumably adding a flag that didn't exist in old versions should be harmless, so I figured it didn't add any value. I guess the self-documenting aspect is worth considering though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't have a strong preference here, but slightly lean towards including it just for readability.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Guard added.