Stop function calling in tabs Stop function calling in tabs json json

Stop function calling in tabs


You can prevent this by setting the OffscreenPageLimit to 2 to your ViewPager instance.Here is the docs -

/**     * Set the number of pages that should be retained to either side of the     * current page in the view hierarchy in an idle state. Pages beyond this     * limit will be recreated from the adapter when needed.     *     * <p>This is offered as an optimization. If you know in advance the number     * of pages you will need to support or have lazy-loading mechanisms in place     * on your pages, tweaking this setting can have benefits in perceived smoothness     * of paging animations and interaction. If you have a small number of pages (3-4)     * that you can keep active all at once, less time will be spent in layout for     * newly created view subtrees as the user pages back and forth.</p>     *     * <p>You should keep this limit low, especially if your pages have complex layouts.     * This setting defaults to 1.</p>     *     * @param limit How many pages will be kept offscreen in an idle state.     */    public void setOffscreenPageLimit(int limit) {        if (limit < DEFAULT_OFFSCREEN_PAGES) {            Log.w(TAG, "Requested offscreen page limit " + limit + " too small; defaulting to " +                    DEFAULT_OFFSCREEN_PAGES);            limit = DEFAULT_OFFSCREEN_PAGES;        }        if (limit != mOffscreenPageLimit) {            mOffscreenPageLimit = limit;            populate();        }    }


If you're retrieving from the DB every time you swipe to C, then you need to clear the list before hand, either in the onResume method when you enter the fragment, or in the onPause method when you leave the fragment.

This mean either clear your ArrayList for example, and make sure its not static, or else it means you have to clear your ListView by using listView.setAdapter(null);. Then don't forget to use notififyDatasetChanged() on the adapter.


Since you haven't pasted your RetrieveData() code, I assume you read the data into an ArrayList<> or iterate over the cursor.And also there are no other places where you are calling this method except onCreateVie(). Some of the possible solutions may be:

  1. If you are explicitly setting viewPager.setOffscreenPageLimit(x), where x>1, remove that code. By default this value is 1 and view Pager calls createView every time.
  2. As you are using FragmentPagerAdapter, once you have loaded your C fragment ideally , it should not be created again, since it should be present in memory and ViewPager will be using that.Check if your onCreateView() is being called every time for C fragment.
  3. Please make sure while retrieving your data into a list every time, you are not appending into list instead of clearing the list and then adding it.

Please try above.