0

My app downloads a PDF file from webserver and opens it with Adobe Reader. But when I click back button on Adobe Reader, my app restarts. I don't want my app to restart when closing Adobe Reader. Note: My app has a file browser dialog, I choose a PDF and display it.

How can I prevent restarting the acitivity when turn back from Adobe Reader?

My code ...

private class DownloadFile extends AsyncTask<String, Void, Void>{

        @Override
        protected Void doInBackground(String... strings) {
            String fileUrl = strings[0];   // -> http://maven.apache.org/maven-1.x/dokuman.pdf
            String fileName = strings[1];  // -> dokuman.pdf
            String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
            File folder = new File(extStorageDirectory, "dokuman");
            folder.mkdir();

            File pdfFile = new File(folder, fileName);

            try{
                pdfFile.createNewFile();
            }catch (IOException e){
                e.printStackTrace();
            }
            Downloader.downloadFile(fileUrl, pdfFile);
            return null;
        }

    }

...

public class Downloader {
    private static final int  MEGABYTE = 1024 * 1024;

    public static void downloadFile(String fileUrl, File directory){
        try {

            URL url = new URL(fileUrl);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            //urlConnection.setRequestMethod("GET");
            //urlConnection.setDoOutput(true);
            urlConnection.connect();

            InputStream inputStream = urlConnection.getInputStream();
            FileOutputStream fileOutputStream = new FileOutputStream(directory);
            int totalSize = urlConnection.getContentLength();

            byte[] buffer = new byte[MEGABYTE];
            int bufferLength = 0;
            while((bufferLength = inputStream.read(buffer))>0 ){
                fileOutputStream.write(buffer, 0, bufferLength);
            }
            fileOutputStream.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

...

new DownloadFile().execute(host+"dene.pdf", "dokuman.pdf");
                        File pdfFile = new File(Environment.getExternalStorageDirectory() + "/dokuman/" + "dokuman.pdf");  // -> filename = dokuman.pdf
                        Uri path = Uri.fromFile(pdfFile);
                        Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
                        pdfIntent.setDataAndType(path, "application/pdf");
                        pdfIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        try{
                        startActivity(pdfIntent);
                        }catch(ActivityNotFoundException e){
                        Toast.makeText(MainActivity.this, "No PDF Viewer App!", Toast.LENGTH_SHORT).show();
...
kadir_cakir
  • 1,013
  • 1
  • 16
  • 22

0 Answers0