title: ActivityResultLauncher<Intent> / Does not create a PDF file / ACTION_CREATE_DOCUMENT
body: <p>I have a problem creating a PDF file with &quot;ACTION_CREATE_DOCUMENT&quot;.
  Creating the PDF file still works with &quot;public void onActivityResult&quot;. However, &quot;public void onActivityResult&quot; has recently been depresated.</p>
  <p>Now I tried to create the PDF file with &quot;ActivityResultLauncher&quot;. A file (0 bytes) is created. I noticed that when creating the file, there is no jump to the try block (createDocument) (jumps to catch).
  How can I process or request the request code with &quot;ActivityResultLauncher&quot;? Or am I completely wrong?</p>
  <p>Example 1, code works.</p>
  <p>Example 2, 0 bytes (jumps in catch, function &quot;createDocument&quot;).</p>
  <hr />
  <hr />
  <p>Example 1 (onActivityResult) works for me:</p>
  <pre><code>//OnStart()
  Intent intentm = new Intent(Intent.ACTION_CREATE_DOCUMENT);
                  intentm.addCategory(Intent.CATEGORY_OPENABLE);
                  intentm.setType(&quot;application/pdf&quot;);
                  intentm.putExtra(Intent.EXTRA_TITLE, dateiname1);
                  startActivityForResult(intentm, WRITE_REQ);//depresated!!!!!!!!!!!!
  
  ___________________________________________________________________________________________
  
   @Override
      public void onActivityResult(int requestCode, int resultCode,
      Intent resultData) {
  
  
          super.onActivityResult(requestCode, resultCode, resultData);
  
          if (resultCode == Activity.RESULT_OK) {
  
              Uri uri = null;
              if (resultData != null) {
  
                  //uri in String speichern um später irgendwo im Code &quot;Speichern&quot; zu können
                  //Siehe Funktion &quot;private void editDocument(Uri uri)&quot;
                  uri = resultData.getData();
                  PfadNeu = uri.toString();
               }
  
             
              if (requestCode == WRITE_REQ) {
                  createDocument(uri);//is working!!!!!!!!!!!!
              }
  
          }
      }
  
  ______________________________________________________________________________________________
  
  
  private void createDocument(Uri uri) {
  
  // create a new document
  
  .....
          PdfDocument document = new PdfDocument();
  
          // crate a page description
          PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(500, 700, 1).create();
  
          // start a page
  
          PdfDocument.Page page = document.startPage(pageInfo);
          Canvas canvas = page.getCanvas();
          Paint paint = new Paint();
  
   canvas.drawText(euroj, 283, 320, paint);
          canvas.drawText(eurof, 283, 335, paint);
          canvas.drawText(euromaer, 283, 350, paint);
          canvas.drawText(euroa, 283, 365, paint);
          canvas.drawText(euromai, 283, 380, paint);
          canvas.drawText(eurojun, 283, 395, paint);
          canvas.drawText(eurojul, 283, 410, paint);
          canvas.drawText(euroaug, 283, 425, paint);
          canvas.drawText(eurosep, 283, 440, paint);
          canvas.drawText(eurookt, 283, 455, paint);
          canvas.drawText(euronov, 283, 470, paint);
          canvas.drawText(eurodez, 283, 485, paint);
          // finish the page
          document.finishPage(page);
  
  
  
          try {
  
              ParcelFileDescriptor fileDescriptor = this.getContentResolver().openFileDescriptor(uri, &quot;w&quot;);
              FileOutputStream fileOutputStream =
                      new FileOutputStream(fileDescriptor.getFileDescriptor());
              document.writeTo(fileOutputStream);
              Toast.makeText(WeitereTools.this,&quot;PDF - Datei wurde erstellt.&quot;, Toast.LENGTH_LONG).show();
  
          } catch (Exception e) {
  
          }
  
          document.close();
  
  }
  
  
  ___________________________________________________________________________________________
  ___________________________________________________________________________________________
  
  Example 2 (ActivityResultLauncher) not works:
  
  //OnStart()
   Intent intentm = new Intent(Intent.ACTION_CREATE_DOCUMENT);
                  intentm.addCategory(Intent.CATEGORY_OPENABLE);
                  intentm.setType(&quot;application/pdf&quot;);
                  intentm.putExtra(Intent.EXTRA_TITLE, dateiname1);
                  someActivityResultLauncher.launch(intentm);//New without request code
  
  _________________________________________________________________________________________
  
   ActivityResultLauncher&lt;Intent&gt; someActivityResultLauncher = registerForActivityResult(
              new ActivityResultContracts.StartActivityForResult(),
              new ActivityResultCallback&lt;ActivityResult&gt;() {
                  @Override
  
                  public void onActivityResult(ActivityResult result) {
  //                    Uri uri=null;
                      if (result.getResultCode() == Activity.RESULT_OK &amp;&amp; result.getResultCode() == WRITE_REQ) {
  
                          if(result.getData() != null) {
  
                              Uri uri=null;
                            uri = result.getData().getData();
                   String   PfadNeu = uri.toString();
  
  
  
                                  
                                      createDocument(uri);
                                  
                                     
  
                              }
                          }
  
  
                   }
  
  
                  }
              });
  
  __________________________________________________________________________________________
  
  private void createDocument(Uri uri) {
  
  // create a new document
  
  .....
          PdfDocument document = new PdfDocument();
  
          // crate a page description
          PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(500, 700, 1).create();
  
          // start a page
  
          PdfDocument.Page page = document.startPage(pageInfo);
          Canvas canvas = page.getCanvas();
          Paint paint = new Paint();
  
   canvas.drawText(euroj, 283, 320, paint);
          canvas.drawText(eurof, 283, 335, paint);
          canvas.drawText(euromaer, 283, 350, paint);
          canvas.drawText(euroa, 283, 365, paint);
          canvas.drawText(euromai, 283, 380, paint);
          canvas.drawText(eurojun, 283, 395, paint);
          canvas.drawText(eurojul, 283, 410, paint);
          canvas.drawText(euroaug, 283, 425, paint);
          canvas.drawText(eurosep, 283, 440, paint);
          canvas.drawText(eurookt, 283, 455, paint);
          canvas.drawText(euronov, 283, 470, paint);
          canvas.drawText(eurodez, 283, 485, paint);
          // finish the page
          document.finishPage(page);
  
  
  
          try {
  
              ParcelFileDescriptor fileDescriptor = this.getContentResolver().openFileDescriptor(uri, &quot;w&quot;);
              FileOutputStream fileOutputStream =
                      new FileOutputStream(fileDescriptor.getFileDescriptor());
              document.writeTo(fileOutputStream);
              Toast.makeText(WeitereTools.this,&quot;PDF - Datei wurde erstellt.&quot;, Toast.LENGTH_LONG).show();
  
          } catch (Exception e) {
  
          }
  
          document.close();
  
  }
  
  
  </code></pre>