When transferring a Bitmap from a fragment to an activation, I found such a bug - I use the FragmentStatePagerAdapter for the ViewPager, when I open the image in a separate window, 3 pages of the ViewPager fragment are written, 1 is the current, 2 is the previous one, 3 is the next, and it turns out that it is recorded and transmitted not the current image and the last third. The way to set viewPager.setOffscreenPageLimit (0) - does not work. Here are the logs:

D/PreviewActivity: onCreate a D/PageFragment: newInstance fragment D/PageFragment: newInstance fragment D/PageFragment: newInstance fragment D/PageFragment: onAttach fragment D/PageFragment: onCreate fragment: 2130837590 D/PageFragment: onAttach fragment D/PageFragment: onCreate fragment: 2130837589 D/PageFragment: onAttach fragment D/PageFragment: onCreate fragment: 2130837591 D/PreviewActivity: onSetImage a D/PageFragment: onCreateView fragment: 2130837590 D/PreviewActivity: onSetImage a D/PageFragment: onCreateView fragment: 2130837589 D/PreviewActivity: onSetImage a D/PageFragment: onCreateView fragment: 2130837591 

Code from the fragment:

  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); pageNumber = getArguments().getInt(ARGUMENT_PAGE_NUMBER); Log.d(TAG, "onCreate fragment: " + pageNumber); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_view_pager, null); ImageView resultView = (ImageView) view.findViewById(R.id.result_image); resultView.setImageResource(pageNumber); final Bitmap bitmap = ((BitmapDrawable) resultView.getDrawable()).getBitmap(); eventListener.onSetImage(bitmap, pageNumber); // pass bitmap to parent ActivityBug Log.d(TAG, "onCreateView fragment: " + pageNumber); return view; } 

When we will do the swipe right-left in this activation, each time I will receive either the next page image or the previous one, because it will always be overwritten, how can I always get the current image of the page, then to transfer it to the parent activation?

    1 answer 1

    If you use heirs of a ListFragment , then in your heir class you can put a Bitmap in the class field and add getter for it:

     public YourFragment extends ListFragment { ... private Bitmap mBitmap; ... public Bitmap getBitmap() { return mBitmap; } ... } 

    Suppose your Activity holds a link to the ViewPager . Then, with the next step inside your used Activity install the following Listener your ViewPager :

     viewPager.addOnPageChangeListener(new SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { FragmentStatePagerAdapter adapter = (FragmentStatePagerAdapter) viewPager.getAdapter(); YourFragment currentFragment = (YourFragment) adapter.instantiateItem(viewPager, position); Bitmap currentBitmap = currentFragment.getBitmap(); // вот текущий Bitmap внутри Activity, используйте его в своих целях } }); 
    • Thanks, good way, it works fine if you turn the pages of the ViewPager, but unfortunately it does not work when we open the image for the first time - because here we get null, I think this is because of the life cycle of the android ... how then to get this damn bitmap when you first open the image?)) - nicolas asinovich
    • I understood everything, at the first opening, only the onPageScrolled method will onPageScrolled so I transferred the code to get the current Bitmap from onPageSelected into it and everything worked as it should))) thanks again! - nicolas asinovich