Error in context FLAG_ACTIVITY_NEW_TASK

In my application, I have an extended list, and I want to open a PDF file downloaded from the Internet when I click on a specific child. When I click on it, the application crashes and this error appears in Android Monitor on Android Studio:

To call the startActivity () function from outside the action context, the FLAG_ACTIVITY_NEW_TASK flag is required. Is this really what you want?

When I try addflag () or setflag (), it tells me something about a static context.

ContextGetter Class:

public class ContextGetter extends Application {
    private static Context context;

    public void onCreate(){
        super.onCreate();
        context = getApplicationContext();
    }

    public static Context getAppContext() {
        return context;
    }
}

Bootloader class:

public class Downloader {
    public static void DownloadFile(String fileURL, File directory) {
        try {
            FileOutputStream f = new FileOutputStream(directory);
            URL u = new URL(fileURL);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();

            InputStream in = c.getInputStream();

            byte[] buffer = new byte[1024];
            int len1 = 0;
            while ((len1 = in.read(buffer)) > 0) {
                f.write(buffer, 0, len1);
            }
            f.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

OpenPDF class (AbrirPDF class in my language):

public class AbrirPDF {
    public static void showPdf() {
        File file = new File(Environment.getExternalStorageDirectory()+File.separator+"lightflow/Read.pdf");
        PackageManager packageManager = ContextGetter.getAppContext().getPackageManager();
        Intent testIntent = new Intent(Intent.ACTION_VIEW);
        testIntent.setType("application/pdf");
        List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        Uri uri = Uri.fromFile(file);
        intent.setDataAndType(uri, "application/pdf");
        ContextGetter.getAppContext().startActivity(intent);
    }
}

Part of the Java activity code:

private void registerClick() {
    expListView.setOnChildClickListener(new OnChildClickListener() {

        @Override
        public boolean onChildClick(ExpandableListView parent, View v,
                                    int groupPosition, int childPosition, long id) {
            if ((groupPosition == 0) && (childPosition == 0)) {
                File file = new File(Environment.getExternalStorageDirectory()+File.separator+"IAVE", "Read.pdf");
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                Downloader.DownloadFile("MY_URL", file);


                AbrirPDF.showPdf();
            } else {

            }

            return false;
        }
    });
}
+4
source share
1 answer

,

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

, Activity showPdf() .

public static void showPdf(Activity activity) {
    ...
    activity.startActivity(intent);
}

onChildClick, :

AbrirPDF.showPdf(MyActivityClassName.this);
+2

Source: https://habr.com/ru/post/1626568/


All Articles