简体   繁体   中英

Activity requires FLAG_ACTIVITY_NEW_TASK flag

I am using Xamarin and my emulator is performing an error when I click on a TextView that has a link for a phone number.

The application output has this output:

[MessageQueue-JNI] Exception in MessageQueue callback: handleReceiveCallback
[MessageQueue-JNI] android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity  context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

Where do I need to set the flag, and what should I set it to?

May I please have some help to get this working?

Thanks in advance.

EDIT

Here is my application code:

namespace TestTextViewAutoLink
{
    [Activity (Label = "TestTextViewAutoLink", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);

            TextView textView = new TextView (this.ApplicationContext);
            textView.AutoLinkMask = Android.Text.Util.MatchOptions.PhoneNumbers; 
            textView.Text = "This is a phone number 0800 32 32 32";

            //Linkify.AddLinks(textView, MatchOptions.PhoneNumbers);

            SetContentView(textView);
        }
    }
}

Where, in the above code, should I place the Intent flag?

EDIT2

Here is my code to start the activity, with ActivityFlags.NewTask

namespace TestTextViewAutoLink
{
    [Activity (Label = "TestTextViewAutoLink", MainLauncher = true)]
    public class MainActivity : Activity
    {
        protected override void OnCreate (Bundle bundle)
        {
            Intent intent= new Intent(this.ApplicationContext, typeof(AutoLinkActivity));
            intent.SetFlags(ActivityFlags.NewTask);
            StartActivity(intent);
        }
    }
}

However, this error now occurs:

android.util.SuperNotCalledException: Activity {TestTextViewAutoLink.TestTextViewAutoLink/testtextviewautolink.MainActivity} did not call through to super.onCreate()

How can I get this code working?

You missed to write call for onCreate function in super class

base.OnCreate (bundle);

protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            Intent intent= new Intent(this.ApplicationContext, typeof(AutoLinkActivity));
            intent.SetFlags(ActivityFlags.NewTask);
            StartActivity(intent);
        }

Try this..

The Error itself having meaning

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

Example

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

Setting flags should be avoided as it will interfere with normal flow of event and history stack.

Hence to get CurrentActivity in Xamarin.Android Project use:

Xamarin.Essentials.Platform.CurrentActivity

//Now you can start an activity as
Xamarin.Essentials.Platform.CurrentActivity.StartActivity(intent);

Note that this might require Xamarin.Essentials v1.5 or above

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM