0

I'm trying to create a PdfDocument with android out side the view of the user. Basically, I want to generate PdfDocument that is different than the view that is currently being displayed. Once this is created, it is emailed to the user. I added all my content that I want in the pdf to an AlertDialog and on the dismiss, I save the view to pdf and send the email.

final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final View dialogView = inflater.inflate(R.layout.receipt_layout, null);
... add stuff to view
builder.setView(dialogView);
final Dialog dialog = builder.create();
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
    @Override
    public void onDismiss(DialogInterface dialog) {
        ... save pdf and send email
    }
 }
 dialog.show();
 WindowManager wm = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
 Display display = wm.getDefaultDisplay();
 DisplayMetrics outMetrics = new DisplayMetrics ();
 display.getMetrics(outMetrics);
 float density  = getResources().getDisplayMetrics().density;
 float dpHeight = outMetrics.heightPixels / density;
 float dpWidth  = outMetrics.widthPixels / density;
 dialog.getWindow().setLayout(370, (int)dpHeight);
 //dialog.dismiss(); // when uncommented, it produces a blank document

This works fine when the alert displays and I manually trigger the dismiss. However, when I add the dialog.dismiss() after showing the dialog, it produces a blank document. I'm not sure if there is an event I can tap into indicating that the view is visible/prime for pdf creation and then trigger the dismiss()? Or maybe there is an easier way to do this outside the view of the user (a flicker is fine)?

bmurmistro
  • 1,070
  • 1
  • 16
  • 35

2 Answers2

0

I would try overriding the onPause() method of the dialog and placing your code to save the screen to a pdf there. When an activity is shut down, the first method called is onPause() and at this point the activity is still visible. The next method called is onStop(); at this point the activity is no longer visible. Dialog.dismiss() is taking the dialog successively through onPause(), onStop() and onDestroy(), so the dialog is no longer around when you get to the listener in your code above.

0

Android: PdfDocument generates empty pdf

I ended up using OnGlobalLayoutListener to trigger the dismiss call.

Community
  • 1
  • 1
bmurmistro
  • 1,070
  • 1
  • 16
  • 35