This question has already been answered:

There is this class:

public class Share implements IShare { @Override public void share(List<File> data) { ArrayList<Uri> toShare = new ArrayList<>(); for(File f : data){ toShare.add(Uri.fromFile(f)); } Intent shareIntent = new Intent(); shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE); shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, toShare); shareIntent.setType("image/*"); Intent myChooser = Intent.createChooser(shareIntent, "Share pictures to: "); myChooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); MyApp.getContext().startActivity(myChooser); IHistoryManager hm = new HistoryManager(); hm.addToHistory(HistoryTypes.SENT, data); } } 

In which chooser flag is set FLAG_ACTIVITY_NEW_TASK, however, after starting the method, an exception is thrown:

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

MyApp is a class inherited from Application; the static getContext () method returns a static variable with the application context:

  public class MyApp extends Application { private static Context context; @Override public void onCreate() { super.onCreate(); context = getApplicationContext(); } public static Context getContext() { return context; } } 

Reported as a duplicate by pavlofff android Aug 6 '16 at 9:02 .

A similar question was asked earlier and an answer has already been received. If the answers provided are not exhaustive, please ask a new question .

    1 answer 1

    Try rewriting your Application class in this way, it can help:

     public class MyApplication extends Application { private static final String TAG = MyApplication.class.getSimpleName(); public static Context context; @Override public void onCreate() { super.onCreate(); MyApplication.context = getApplicationContext(); } public static Context getContext() { return MyApplication.context; } } 
    • No, it did not help, it still throws an exception - Shadasviar
    • instead of .setFlags (Intent.FLAG_ACTIVITY_NEW_TASK); try .addFlags (Intent.FLAG_ACTIVITY_NEW_TASK); - Kirill Stoianov
    • In the debugger, it falls out on the line Intent myChooser = Intent.createChooser (shareIntent, "Share pictures to:"); that is, it does not reach the flag setting - Shadasviar
    • and what does the code do to you? Share pictures? - Kirill Stoianov