Flutter: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag

2,370

Just had the same problem with the new Version of the urlLauncher 4.0.2 Plugin

I downgraded to 3.0.3 and everything worked fine so there might be a bug inside the repository.

pubspec.yaml

 url_launcher: 3.0.3

Sample Code (taken from the git repo but it works for me with 3.0.3)

https://github.com/flutter/plugins/tree/master/packages/url_launcher

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';

void main() {
  runApp(Scaffold(
    body: Center(
      child: RaisedButton(
        onPressed: _launchURL,
        child: Text('Show Flutter homepage'),
      ),
    ),
  ));
}

_launchURL() async {
  const url = 'https://flutter.io';
  if (await canLaunch(url)) {
    await launch(url);
  } else {
    throw 'Could not launch $url';
  }
}

For all avaiable versions of the Plugin see here. https://pub.dartlang.org/packages/url_launcher#-changelog-tab-

Share:
2,370
Vishnu
Author by

Vishnu

Updated on December 07, 2022

Comments

  • Vishnu
    Vishnu over 1 year

    I'm trying to

    launch("tel://21213123123")
    

    But, I'm getting the following error!

    PlatformException (PlatformException(error, Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?, null))
    

    in this file

    message_codecs.dart
    

    Here is my error log

    E/MethodChannel#plugins.flutter.io/url_launcher(26131): Failed to handle method call
    E/MethodChannel#plugins.flutter.io/url_launcher(26131): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at android.app.ContextImpl.startActivity(ContextImpl.java:672)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at android.app.ContextImpl.startActivity(ContextImpl.java:659)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at android.content.ContextWrapper.startActivity(ContextWrapper.java:331)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at io.flutter.plugins.urllauncher.UrlLauncherPlugin.onMethodCall(UrlLauncherPlugin.java:61)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:200)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at io.flutter.view.FlutterNativeView.handlePlatformMessage(FlutterNativeView.java:163)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at android.os.MessageQueue.nativePollOnce(Native Method)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at android.os.MessageQueue.next(MessageQueue.java:323)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at android.os.Looper.loop(Looper.java:135)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at android.app.ActivityThread.main(ActivityThread.java:5468)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at java.lang.reflect.Method.invoke(Native Method)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
    E/MethodChannel#plugins.flutter.io/url_launcher(26131):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:671)
    
  • Jose Jet
    Jose Jet over 5 years
    @ManuelBechmann thank you! this solve the problem, hope they solve it for newer versions aswell