7

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

StuStirling
  • 15,601
  • 23
  • 93
  • 150
  • Why do you want to get the bitmap of the fragment's view before actually adding it to the visible screen? – user Feb 06 '13 at 19:58
  • I want the view because I want what its displaying to convert it into a bitmap which i can then upload to Facebook – StuStirling Feb 06 '13 at 22:33
  • I understood that, the question was **why** the need to create that bitmap **before** the user will see it(which I'm assuming will happen at a later point when you do add the fragment to the layout)? – user Feb 07 '13 at 08:57
  • I don't want the user to see the fragment at all, I only want to create this fragment to get its view and then destroy it again. The fragment is another display elsewhere in the application. – StuStirling Feb 07 '13 at 10:29
  • 1
    Then you don't have any alternative for measuring and drawing the view yourself, that fragment it's outside of the visual screen. – user Feb 07 '13 at 11:38
  • Luksprog please post your answer as it is correct. – StuStirling Feb 27 '13 at 11:21
  • There is no need for me to post an answer, a simple statement(in the form of a self accepted answer) from you mentioning what you found out is enough. – user Feb 28 '13 at 10:48

4 Answers4

3

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
Community
  • 1
  • 1
Brian Attwell
  • 9,239
  • 2
  • 31
  • 26
1

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

Community
  • 1
  • 1
D-Dᴙum
  • 7,689
  • 8
  • 58
  • 97
  • unfortunately not. Thanks though! – StuStirling Feb 04 '13 at 16:45
  • What my answer hints at is instantiating the Fragment in code and then setting its visibility with either show() or hide(). That way you have the Frgament available to you but not visible to the user. – D-Dᴙum Feb 12 '13 at 08:46
1

Is there any way you can just add your Fragments to a hidden layout? Example XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/GoneFragmentContainer"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:visibility="gone" />

Then in your code:

MyFragment myFragment = new MyFragment();
getSupportFragmentManager().beginTransaction()
  .add(R.id.GoneFragmentContainer, myFragment)
  .commit();
myFragment.getView().callMethodsOnMyNonVisibleFragment();
user697495
  • 590
  • 1
  • 3
  • 9
  • I think in case the parent viewgroup has visibility=gone, the fragment's onCreateView will never be called. – Tapemaster Sep 12 '13 at 10:42
1

I would try to avoid grabbing the Bitmap from a View and instead get it some other way. Then you can do whatever processing you need in the onAttach() method of the fragment, which gets called when you instantiate the fragment in its parent activity and it does that before it's visible.

Wenger
  • 989
  • 2
  • 12
  • 35
  • So I'm going to have to draw it myself? – StuStirling Feb 08 '13 at 14:40
  • The fragment's views aren't created until the onCreateView() method. So if you want to get a bitmap from a fragment's view, that bitmap has to come from somewhere before it's put into an ImageView. I'm saying, do that "getting/processsing the bitmap from somewhere" in the onAttach() method so you might be able to get it before the fragment's view is inflated. – Wenger Feb 08 '13 at 14:54