Viewpager snaps to the wrong page after taking a photo <p>I am currently using a <strong>Viewpager</strong> implementation in my android app. In each of the fragment, there is a <strong>button</strong> that takes a photo and uploads it to an online database. However, due to the new permissions requirement, I needed to ask for permission. Hence, i would need to use the <strong><em>onRequestPermissionsResult()</em></strong> to continue uploading.</p> <p>Below is my implementation</p> <p>this is from the <strong>ViewPager.class</strong> (Since after permission is granted, this method in the Viewpager is called instead of the one in the fragment)</p> <pre><code>@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_TAKE_PHOTO: if (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) { //Permission granted to write and read file //Get the current fragment List&lt;Fragment&gt; fragments = getSupportFragmentManager().getFragments(); for (Fragment currFragment : fragments) { if (currFragment != null) currFragment.onRequestPermissionsResult( REQUEST_TAKE_PHOTO , permissions , grantResults); } } return; default: //Do nothing return; } } </code></pre> <p>this is from the <strong>fragment</strong> itself</p> <pre><code>@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case REQUEST_TAKE_PHOTO: if (grantResults.length &gt; 0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) { //Permission granted to write and read file uploadImage(); } return; default: //Do nothing return; } } </code></pre> <p>this part</p> <pre><code>List&lt;Fragment&gt; fragments = getSupportFragmentManager().getFragments(); for (Fragment currFragment : fragments) { if (currFragment != null) currFragment.onRequestPermissionsResult( REQUEST_TAKE_PHOTO , permissions , grantResults); } </code></pre> <p>is my attempt to find the <strong>correct fragment</strong> that clicked the button and call the <strong>OnRequestPermissionResult()</strong> method.</p> <p>However, I notice this weird behavior from <strong>Viewpager</strong>. The way I implemented it is that I click a <strong>Viewholder</strong> from a <strong>RecyclerView</strong>, then that enters that position in the ViewPager.</p> <p>The weird behavior is that for example, I entered page 2 from the <strong>RecyclerView</strong>. Then i swipe to page 4 and click the upload photo button, after the photo is done(via the in-build camera app), it will switch back to page 2.</p> <p>Why is that? How do I fix it? Sorry for a lengthy post. Just trying to get as much information out there. Thanks</p> <p>EDIT: This is how I call the requestPermission</p> <pre><code>@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode != Activity.RESULT_OK) { return; } if (requestCode == REQUEST_TAKE_PHOTO) { //Request for permission to access file if (ContextCompat.checkSelfPermission(getActivity(), android.Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { //ask for permission ActivityCompat.requestPermissions(getActivity(), new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_TAKE_PHOTO); } else { uploadImage(); } } } </code></pre> <p>Is something wrong?</p> <p>Edit Post: This is the part where I called the notifyDataSetChange()</p> <pre><code>private void loadData() { mRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { ArrayList&lt;RouteInstructions&gt; mList2 = new ArrayList&lt;RouteInstructions&gt;(); for (DataSnapshot instructions : dataSnapshot.getChildren()) { mList2.add(instructions.getValue(RouteInstructions.class)); } mList = mList2; mAdapter.notifyDataSetChanged(); mPager.setCurrentItem(getIntent().getIntExtra(EXTRA_INDEX, 0)); } @Override public void onCancelled(DatabaseError databaseError) { } }); } </code></pre>