Creating and using a fragment view, although not visible to the user

Cause

I need to do this because I want to get an accessible fragment view cache, and then create a Bitmap from this view. This Bitmap will be used for publication on Facebook.

Problem

When I create my fragment, views are not created because they were not added through the fragment transaction and because the displayed view is not displayed to the user.

I also do not want to draw these views manually, since I will not get an exact copy of the screen.

My question

Is there a way to have full functionality (same functionality as if you were adding it through FragmentTransaction) of a fragment without actually showing the fragment?

I need to have access to the fragment view when it is not displayed.

thanks in advance

+7
source share
4 answers

Another question is about rendering activity in offscreen buffers . You can use this method to render a fragment to a buffer off-screen.

The method includes:

  • start a new activity using startActivityForResult () instead of startActivity ()
  • configure drawing cache
  • use getDrawingBitmap () after layout completion
+3
source

Is the answer I gave here also relevant to your situation? It mainly uses different FragmentTransaction methods.

+1
source

Can I add fragments to a hidden layout? XML example:

<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(); 
+1
source

I would try to avoid capturing Bitmap from View and get it in another way instead. Then you can perform any necessary processing in the onAttach() method of the fragment that is called when the instance is created in its parent activity, and it does this before it becomes visible.

+1
source

All Articles