title: Android ViewPager current page update happens in the next page
body: <p>I'm new to Android and I'm using API version 4.4. I'm using a ViewPager to display a fragment exactly as described in this link. <a href="http://developer.android.com/training/animation/screen-slide.html" rel="nofollow">http://developer.android.com/training/animation/screen-slide.html</a>.</p>
  
  <p>What my app does is it let the user to take a picture from camera and when he clicks the next button he can add another image and he can go on keep doing that(also he can come to the previous page). I'm displaying his picture inside a imageView.</p>
  
  <p>I update the imageView in the fragment in the <code>onActivityResult</code> method inside Fragment. The issue is, when I change imageView (To his camera picture) it happesn to the next page in the viewPager not in the current page. Also what I noticed is <code>onResume</code> function in my Fragment class fires multiple times when the page loads for the first time.</p>
  
  <p>This is my code so far,</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>private static final int MAX_IMAGES = 10;
  protected int currentIndex=0; // Which page we are currently in
  private ImageFragment fragment;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_activity);
  
      viewPager = (ViewPager)findViewById(R.id.viewPager);
      pageAdapter = new TicketImageAdapter(getSupportFragmentManager());
      viewPager.setAdapter(pageAdapter);
  
      //LinearLayout uploadImage  = ((LinearLayout)findViewById(R.id.uploadImage));
      Button nextButton           = (Button)findViewById(R.id.nextBttn);
      Button saveButton           = (Button)findViewById(R.id.saveButton);
      Button previousButton       = (Button)findViewById(R.id.previousBttn); 
  
      nextButton.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
  
              if(fragment != null)
              {
                  fragment.addTicketImage();
                  displayTicketImages();
              }
  
              if(MAX_IMAGES &gt; currentIndex)
              {
                  currentIndex++;
                  Log.d("Currect Index; ",currentIndex+"");
                  setCurrentItem (currentIndex, true);
              }
          }
      });
  // Set current page fragment so I can access it's methods
  public void setFragment(TicketImageFragment fragment)
  {
      this.fragment = fragment;
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      //Don't worry about fragment been null, coz it is not. I Set the fragment in the on
      if(this.fragment != null)
      {
          this.fragment.onActivityResult(requestCode, resultCode, data);
      }
  }
  
     public static class TicketImageAdapter extends FragmentPagerAdapter {
  
      public TicketImageAdapter(FragmentManager fragmentManager) {
          super(fragmentManager);
      }
  
      @Override
      public int getCount() {
          return MAX_IMAGES;
      }
  
      @Override
      public Fragment getItem(int position) {
          return new ImageFragment();
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p><strong>ImageFragment.java</strong></p>
  
  <p>This is where I redirect the common user to the camera and display his picture like a preview in a imageView.</p>
  
  <pre><code>private TextView firstText;
  private MainActivity activity;
  View view;
  ImageView imgPreview;
  
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
  {
      view = inflater.inflate(R.layout.activity_fragment,container,false);
  
      LinearLayout uploadImageBttn = ((LinearLayout)view.findViewById(R.id.uploadImageButton));
      firstText                    = ((TextView)view.findViewById(R.id.note));
  
  
  
      imgPreview = (ImageView)view.findViewById(R.id.ticketImage);
  
      uploadImageBttn.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              activity.helper = new CameraHelper(v.getContext());
              activity.helper.captureImage(); // Capture image and save it
          }
      });
  
      this.activity  = (TicketImageActivity)getActivity();
  
      // Set fragment So the activity can call onActivityResult on this fragment
      this.activity.setFragment(this);
  
      return view;
  }
  
  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
  
      if(imgPreview == null)
          Log.d("imagePreviewView", "NULL");
      else
          Log.d("UploadButton", "NOT NULL");
  
      // if the result is capturing Image
      if (requestCode == CameraHelper.getCode()) {
          if (resultCode == Activity.RESULT_OK) {
  
              //ImageView imgPreview = (ImageView)view.findViewById(R.id.ticketImage);
              previewCapturedImage(imgPreview,"this is the image path");// Preview captured image
  
          } else if (resultCode == Activity. RESULT_CANCELED) {
              // user cancelled Image capture
              Toast.makeText(view.getContext(),
                      "User cancelled image capture", Toast.LENGTH_SHORT)
                      .show();
          } else {
              // failed to capture image
              Toast.makeText(view.getContext(),
                      "Sorry! Failed to capture image", Toast.LENGTH_SHORT)
                      .show();
          }
      }
  }
   public void previewCapturedImage(ImageView imgPreview,String imagePath) {
      try {
  
          imgPreview.setVisibility(View.VISIBLE); // If the image view is hidden, show it
          BitmapFactory.Options options = new BitmapFactory.Options();
          options.inSampleSize = 8;
  
          try {
              Bitmap bitmap = BitmapFactory.decodeFile(mImagePath,
                      options);
              ExifInterface exif = new ExifInterface(mImagePath);
              int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
              Matrix matrix = new Matrix();
  
              if (orientation == 6) {
                  matrix.postRotate(90);
              }
              else if (orientation == 3) {
                  matrix.postRotate(180);
              }
              else if (orientation == 8) {
                  matrix.postRotate(270);
              }
              bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); // rotating bitmap
              imgPreview.setImageBitmap(bitmap);
          }
          catch (Exception e) {
              Log.d("Exception :",e.getMessage());
          }
          //date = null;
  
      } catch (NullPointerException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p><strong>main_activity.xml</strong></p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical" &gt;
  
  &lt;android.support.v4.view.ViewPager
      android:id="@+id/viewPager"
      android:layout_width="wrap_content"
      android:layout_height="400dp"/&gt;
  
  &lt;LinearLayout android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="horizontal"&gt;
  
      &lt;Button
          android:id="@+id/previousBttn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_gravity="left"
          android:text="@string/Previous" /&gt;
  
      &lt;Button
          android:id="@+id/saveButton"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_gravity="center"
          android:text="@string/saveButton" /&gt;
  
      &lt;Button
          android:id="@+id/nextBttn"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_weight="1"
          android:layout_gravity="right"
          android:text="@string/Next" /&gt;
  &lt;/LinearLayout&gt;    
  </code></pre>
  
  <p></p>
  
  <p><strong>activity_fragment.xml</strong></p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
      &lt;LinearLayout android:layout_width="match_parent"
          android:layout_height="320dp"
          android:orientation="vertical"
          android:id="@+id/uploadImageButton"
          android:background="@drawable/customborder"&gt;
  
           &lt;TextView android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="@string/uploadImageText"/&gt;
  
           &lt;ImageView
              android:id="@+id/ticketImage"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_margin="5dp"
              android:layout_gravity="center"/&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;EditText
          android:id="@+id/note"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:inputType="text"
          android:hint="@string/enterNote" /&gt;
  </code></pre>
  
  <p>Image capturing and saving it in the album works perfectly fine. The only issue is the image is displayed in the wrong page. When I Capture the image in the first page it displays the preview in the second page and when I capture it in the another page it shows in the next page. </p>
  
  <p>Any help would be much appreciated. Thanks!</p>