title: ViewPager and ImageGallery
body: <p>The problem is this:</p>
  <p>When you first choose a picture - everything is fine. If you swipe, and press the &quot;back&quot; button, after you open another image then will open last image which will opened.</p>
  <p>The correct position is passed through the Bundle. I don’t understand where to look for the error.</p>
  <pre><code>    public class ViewPagerAdapter extends PagerAdapter {
  
      private LayoutInflater layoutInflater;
  
      public ViewPagerAdapter() {
      }
  
      @NonNull
      @Override
      public Object instantiateItem(@NonNull ViewGroup container, int position) {
          layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
          View view = layoutInflater.inflate(R.layout.image_fullscreen, container, false);
          ImageView imageView = view.findViewById(R.id.image_fScreen);
          Hit hit = hits.get(position);
  
          Picasso.get().load(hit.getLargeImageURL())
                  .into(imageView);
  
          container.addView(view);
          return view;
      }
  
      @Override
      public int getCount() {
          return hits.size();
      }
  
      @Override
      public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
          return view == object;
      }
  
      @Override
      public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
          container.removeView((View) object);
      }
  }
  </code></pre>
  <p>DialogFragment:</p>
  <pre><code>public class FullScreenDialogFragment extends DialogFragment {
  private ArrayList&lt;Hit&gt; hits;
  private ViewPager viewPager;
  private ViewPagerAdapter viewPagerAdapter;
  private TextView author;
  private TextView count;
  private int selectedPosition;
  private Button buttonShare;
  private static FullScreenDialogFragment fullScreenDialogFragment;
  private int rlyPosition;
  
  public static FullScreenDialogFragment getInstance() {
      if (fullScreenDialogFragment == null) {
          fullScreenDialogFragment = new FullScreenDialogFragment();
      }
      return fullScreenDialogFragment;
  }
  
  @Override
  public void onCreate(@Nullable Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DialogStyle);
  }
  
  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.image_viewpager, container, false);
      viewPager = view.findViewById(R.id.viewpager);
      author = view.findViewById(R.id.author);
      buttonShare = view.findViewById(R.id.btnShare);
      count = view.findViewById(R.id.count);
      hits = (ArrayList&lt;Hit&gt;) getArguments().getSerializable(&quot;hits&quot;);
      selectedPosition = getArguments().getInt(&quot;position&quot;);
  
      viewPagerAdapter = new ViewPagerAdapter();
      viewPager.setAdapter(viewPagerAdapter);
      viewPager.addOnPageChangeListener(onViewPageChangeListener);
  
     
  
      setItem(selectedPosition);
      return view;
  }
  
  private void setItem(int pos) {
      viewPager.setCurrentItem(pos, true);
      setItemInfo(selectedPosition);
  
  }
  
  private void setItemInfo(int pos) {
      count.setText((pos + 1) + &quot; of &quot; + hits.size());
  
      Hit hit = hits.get(pos);
      author.setText(hit.getUser());
  
  
  
  }
  
  
  ViewPager.OnPageChangeListener onViewPageChangeListener = new ViewPager.OnPageChangeListener() {
      @Override
      public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
  
  
      }
  
      @Override
      public void onPageSelected(int position) {
          setItemInfo(position);
  
  
  
      }
  
      @Override
      public void onPageScrollStateChanged(int state) {
  
      }
  };
  </code></pre>