Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and Using a Fragment's View Whilst Not Visible To The User

The Reason

I need to do this because I want to get the drawable cache of the fragment's view and then create a Bitmap from that view. This Bitmap will then be used to publish on Facebook.

The Problem

When I create my Fragment's the views are not being created because they haven't been added via a Fragment Transaction and because the view's they contain aren't visible to the user.

I also do not want to have to draw these views manually as I will not get an exact replica of the screen.

My Question

Is there a way to have full functionality (the same functionality as if you were to add it via a FragmentTransaction) of the Fragment without the fragment actually being shown?

I need to be able to access the fragment's view when it isn't visible.

Thanks In Advance

like image 803
StuStirling Avatar asked Jan 31 '13 17:01

StuStirling


2 Answers

Another question addresses how to render activities into offscreen buffers. You can use this technique to render a fragment into an offscreen buffer.

The technique involves:

  • launching the new activity with startActivityForResult() instead of startActivity()
  • setup a drawing cache
  • use getDrawingBitmap() once layout is finished
like image 169
Brian Attwell Avatar answered Nov 29 '22 11:11

Brian Attwell


Does the answer I gave here also apply to your situation too? Basically it uses different methods of FragmentTransaction.

like image 43
D-Dᴙum Avatar answered Nov 29 '22 11:11

D-Dᴙum