Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

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)
like image 928
Vishnu Avatar asked Nov 27 '18 10:11

Vishnu


People also ask

Should flag_activity_new_task be set when starting an activity?

What you were attempting to do with FLAG_ACTIVITY_NEW_TASK should be reserved for rare cases like when you need to launch an Activity from a Service. But it is not necessary or desirable to set that flag when starting an Activity from within a UI context.

How to call startactivity from a fragment?

If you call startActivity from Fragment, a caller can often be a fragment, not an activity. In my opinion, it's better to use the method of startActivity () just in the your code of the Activity.class. If you use that in the Adapter or other class, it will result in that. I also had the same problem. Check all the context that you have passed.

Why does startactivity() fail to start?

This error goes when startactivity doesn't know which is his activity. So you must add activity before startActivity () If you call startActivity from Fragment, a caller can often be a fragment, not an activity. In my opinion, it's better to use the method of startActivity () just in the your code of the Activity.class.

How to call an activity from a non-activity scenario?

"ActivityClassName.this" (While you pass the context in this manner it will contain all the detail and info that you need to call an Activity from a non-activity scenario) So there is no need to set or add flags, this will work fine in every case.


1 Answers

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-

like image 113
Manuel Bachmann Avatar answered Oct 14 '22 09:10

Manuel Bachmann