FragmentStatePagerSupport中onActivityResult里viewPager.setCurrentItem失效问题
viewPager.setCurrentItem() Not Working in onActivityResult Hey there! Let's troubleshoot why your viewPager.setCurrentItem() call isn't taking effect in onActivityResult—even though your Toast is showing the correct page number, so we know the page data is coming through correctly. I've dealt with this exact issue before, so here are the most likely fixes to try:
1. Check if your ViewPager reference is valid
First, make sure the ViewPager instance you're calling setCurrentItem() on isn't null, and that it's actually attached to the view hierarchy. Sometimes, if your activity/fragment was recreated (like during a screen rotation), old references might point to a detached view. You can add a quick check before calling the method:
if (viewPager != null && viewPager.getParent() != null) { // Proceed with setCurrentItem } else { Toast.makeText(this, "ViewPager isn't ready yet!", Toast.LENGTH_SHORT).show(); }
2. Delay the setCurrentItem() call with post()
The most common culprit here is that onActivityResult fires before the ViewPager (or its adapter) has fully restored its state. By posting the operation to the main thread's message queue, we let the system finish setting up the view hierarchy first:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == YOUR_REQUEST_CODE) { int pageNo = data.getIntExtra("page_number_key", 0); Toast.makeText(this, "pageNo is " + pageNo, Toast.LENGTH_SHORT).show(); // Delay the call until the view is fully ready viewPager.post(new Runnable() { @Override public void run() { // The second parameter enables smooth scrolling (optional) viewPager.setCurrentItem(pageNo, true); } }); } }
3. Verify your PagerAdapter's getCount() returns the correct value
Your ViewPager has 142 pages, so double-check that your FragmentStatePagerAdapter's getCount() method returns 142. If getCount() returns a number smaller than the page number you're trying to jump to, setCurrentItem() will fail silently (no crash, just no page change). Add a safety check to avoid this:
int pageCount = viewPager.getAdapter() != null ? viewPager.getAdapter().getCount() : 0; if (pageNo >= 0 && pageNo < pageCount) { viewPager.post(() -> viewPager.setCurrentItem(pageNo)); } else { Toast.makeText(this, "Invalid page number!", Toast.LENGTH_SHORT).show(); }
4. Make sure you're handling onActivityResult in the right place
If FragmentStatePagerSupport is a Fragment (not an Activity), note that:
- You need to call
startActivityForResult()from the Fragment instance, not the parent Activity. - You should override
onActivityResult()directly in the Fragment, not the Activity.
If you were handling the result in the parent Activity instead of the Fragment, the ViewPager reference might not point to the correct instance in the recreated Fragment. Move the result handling logic into the Fragment's onActivityResult() method, then apply the post() fix above.
5. Re-acquire the ViewPager reference if needed
If your activity/fragment was destroyed while MyListActivity was open (due to low memory), your existing ViewPager reference might be stale. Instead of relying on a cached reference, re-find the ViewPager in onActivityResult:
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK && requestCode == YOUR_REQUEST_CODE) { int pageNo = data.getIntExtra("page_number_key", 0); ViewPager freshViewPager = findViewById(R.id.your_viewpager_id); if (freshViewPager != null) { freshViewPager.post(() -> freshViewPager.setCurrentItem(pageNo)); } } }
Final Notes
In most cases, the post() trick will resolve the issue since it ensures the ViewPager is fully initialized before trying to change the page. Combine that with checks for valid page numbers and ViewPager references, and you should be good to go!
内容的提问来源于stack exchange,提问作者Ibrahim

