Skip to content
Advertisement

Using Intent.createChooser and getting error: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag

I want to open the share via option from the service class. It is working fine in Android 7, but in 8+ OS it starts showing

android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?

I have also included this flag to my Intent, but it’s still showing the same error.

Is there any other way to open share via option from the service class?

JavaScript

Advertisement

Answer

Intent.createChooser creates an Intent, so you need to set the FLAG_ACTIVITY_NEW_TASK flag on that intent, e.g.,

JavaScript

You were also calling startService instead of startActivity – make sure to correct that as well.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement