redstonedev
redstonedev

Reputation: 119

How To Pass A Bitmap From Activity To A Fragment?

I need to create a bitmap from the view of my first activity and then pass that bitmap to a fragment in a ViewPager to use it as said fragment's background.

(using this to create the bitmap: Converting a view to Bitmap without displaying it in Android?).

How would I go about this?

-R

Upvotes: 0

Views: 1521

Answers (2)

Pongpat
Pongpat

Reputation: 13348

You can get bitmap from view by call view.setDrawingCacheEnabled(true) then using

Bitmap bitmap = rootView.getDrawingCache();

You can get Fragment by tag then call some method in fragment and set bitmap to it.

Upvotes: 1

CommonsWare
CommonsWare

Reputation: 1006624

Call a method on your fragment that takes a Bitmap as a parameter, if the fragment already exists. Or, use the factory pattern (static newInstance() method) to pass in the Bitmap if you are creating the fragment at the same time as you are associating the Bitmap with it.

However, you may want to consider having the fragment know how to load the bitmap itself from some simpler identifier (e.g., Uri). The fragment, by default, will be destroyed and recreated on a configuration change, and if you are using FragmentStatePagerAdapter, the fragment will be destroyed and recreated as the user navigates through the pager. This will be far simpler to handle if the fragment can load the image itself.

Upvotes: 1

Related Questions