title: Missing data depending on the number of tabs
body: <p>I want to create multiple tabs depending on number of items retained from API. I have successfully created tabs, and swiping over is working fine, but the issue is that for instance, let's say I have 3 tabs. When they are all created, first and second tabs are empty, and the third has data, when I swipe back to 1, I see the data loaded too, but never on the second tab. When the tabs are 2, neither show data. Here is my code:</p>
  
  <p><strong>Activity</strong></p>
  
  <pre><code>public class MenuActivity extends AppCompatActivity {
  
  
      private ViewPager tabsPager;
      private Toolbar toolbar;
      private ImageView toolbarImageView;
      private TextView subTitleView, titleView, priceCounterView, itemsCounterView;
      private RatingBar ratingBar;
      private TabLayout tabLayout;
      private LinearLayout cartButton;
      private AVLoadingIndicatorView avi;
      private MenuPagerAdapter tabsAdapter;
      public List&lt;MenuType&gt; menuTypes = new ArrayList&lt;&gt;();
      public List&lt;MenuItem&gt; menuItems = new ArrayList&lt;&gt;();
      private Restaurant restaurant;
      private Context context;
      private CollapsingToolbarLayout collapsingToolbarLayout;
      private AppBarLayout appBarLayout;
      private int i = -1;
      public List&lt;Fragment&gt; fragments = new ArrayList&lt;&gt;();
      private int cartCount = 0;
      private int priceCount = 0;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_menu);
          context = this;
  
          tabsPager = (ViewPager) findViewById(R.id.tabs_pager);
          toolbar = (Toolbar) findViewById(R.id.toolbar);
          tabLayout = (TabLayout) findViewById(R.id.tab_layout);
          avi = (AVLoadingIndicatorView) findViewById(R.id.avi);
          titleView = (TextView) findViewById(R.id.titleView);
          subTitleView = (TextView) findViewById(R.id.subTitle);
          ratingBar = (RatingBar) findViewById(R.id.ratingBar);
          toolbarImageView = (ImageView) findViewById(R.id.restoIcon_toolbar);
          appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
          collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.toolbar_layout);
          priceCounterView = (TextView) findViewById(R.id.items_price_counter);
          itemsCounterView = (TextView) findViewById(R.id.items_counter);
          cartButton = (LinearLayout) findViewById(R.id.view_order_button);
  
          if (cartCount == 0 &amp;&amp; priceCount == 0){
              cartButton.setVisibility(View.GONE);
          }
  
  
          setSupportActionBar(toolbar);
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          getSupportActionBar().setDisplayShowHomeEnabled(true);
  
          tabsPager.setCurrentItem(0);
  
          restaurant = (Restaurant) getIntent().getSerializableExtra("restaurant");
  
          collapsingToolbarLayout.setExpandedTitleColor(getResources().getColor(R.color.totalTransparent));
  
          getSupportActionBar().setTitle(restaurant.getName());
          titleView.setText(restaurant.getName());
          subTitleView.setText(restaurant.getSlogan());
          ratingBar.setRating(Float.parseFloat(restaurant.getRating()));
  
          Picasso.with(context)
                  .load(restaurant.getImageURL())
                  .placeholder(R.drawable.restaurant_placeholder)
                  .into(toolbarImageView);
  
          loadMenuTypes();
  
      }
  
      //LOAD MENU TYPES
  
      public void loadMenuTypes(){
  
          RequestQueue queue = Volley.newRequestQueue(this);
          String url = getResources().getString(R.string.api) + "get-menu-types";
          final StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                  new Response.Listener&lt;String&gt;()
                  {
                      @Override
                      public void onResponse(String response) {
                          try {
                              JSONObject jsonObject = new JSONObject(response);
                              JSONArray array = jsonObject.getJSONArray("menu_types");
  
                              for (int i = 0; i &lt; array.length(); i++){
                                  JSONObject restoMainMenu = array.getJSONObject(i);
  
                                  if (restoMainMenu.getBoolean("hasData")) {
                                      MenuType menuType = new MenuType();
  
                                      menuType.setMenuIcon(restoMainMenu.getString("icon"));
                                      menuType.setRestaurant(restaurant);
                                      menuType.setRmenu(restoMainMenu.getString("title"));
                                      menuType.setMenuId(restoMainMenu.getString("id"));
  
                                      menuTypes.add(menuType);
                                  }
                              }
  
  
                          } catch (JSONException e) {
                              Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
                          }
  
                          loadMenuItems();
  
                      }
                  },
                  new Response.ErrorListener()
                  {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          menuTypes.clear();
                          menuItems.clear();
                          loadMenuTypes();
                      }
                  }
          ) {
              @Override
              protected Map&lt;String, String&gt; getParams()
              {
                  Map&lt;String, String&gt;  params = new HashMap&lt;String, String&gt;();
                  params.put("UID", restaurant.getId());
  
                  return params;
              }
          };
          queue.add(postRequest);
      }
  
      //LOAD MENUS
  
      public void loadAllMenu(final MenuType mMenuType){
  
          RequestQueue queue = Volley.newRequestQueue(this);
          String url = getResources().getString(R.string.api) + "get-all-menu";
          StringRequest postRequest = new StringRequest(Request.Method.POST, url,
                  new Response.Listener&lt;String&gt;()
                  {
                      @Override
                      public void onResponse(String response) {
                          try {
                              JSONObject jsonObject = new JSONObject(response);
                              JSONArray array = jsonObject.getJSONArray("detailed_menus");
                              List&lt;MenuItem&gt; menuItems1 = new ArrayList&lt;&gt;();
                              for (int i = 0; i&lt; array.length(); i++){
                                  JSONObject restoDetailMenu = array.getJSONObject(i);
                                  MenuItem item = new MenuItem(
                                          restaurant.getId(),
                                          restoDetailMenu.getString("Name"),
                                          restoDetailMenu.getString("id"),
                                          restoDetailMenu.getString("Price"),
                                          restoDetailMenu.getString("Description"),
                                          restoDetailMenu.getString("AvailableIn"),
                                          restoDetailMenu.getString("Icon")
  
                                  );
  
  
                                  menuItems.add(item);
                                  menuItems1.add(item);
                              }
                              mMenuType.setMenuItems(menuItems1);
  
                          }
  
                          catch (JSONException e) {
                              Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
  
                          }
                      }
                  },
                  new Response.ErrorListener()
                  {
                      @Override
                      public void onErrorResponse(VolleyError error) {
                          loadAllMenu(menuTypes.get(i));
                          Toast.makeText(context, error.getMessage(), Toast.LENGTH_SHORT).show();
  
                      }
                  }
          ) {
              @Override
              protected Map&lt;String, String&gt; getParams()
              {
                  Map&lt;String, String&gt;  params = new HashMap&lt;String, String&gt;();
                  params.put("MenuTypeId", mMenuType.getMenuId());
                  params.put("restId", mMenuType.getRestaurant().getId());
  
  
                  return params;
              }
          };
          queue.add(postRequest);
      }
  
      //TRIGER ALL MENU LOADER
      public void loadMenuItems(){
  
          for (int j = 0; j &lt; menuTypes.size(); j++) {
              i = j;
              loadAllMenu(menuTypes.get(j));
          }
  
          avi.setVisibility(View.GONE);
          setUpTabs();
      }
  
      public void setUpTabs(){
          for (int j = 0; j &lt; menuTypes.size(); j++) {
              tabLayout.addTab(tabLayout.newTab().setText(menuTypes.get(j).getRmenu()));
              MenuFragment fragment = new MenuFragment();
              Bundle bundle = new Bundle();
              bundle.putSerializable("data", menuTypes.get(j));
              fragment.setArguments(bundle);
              fragments.add(MenuFragment.newInstance(menuTypes.get(j)));
          }
  
          tabsAdapter = new MenuPagerAdapter(getSupportFragmentManager(), menuTypes);
          tabsAdapter.notifyDataSetChanged();
          tabsPager.setAdapter(tabsAdapter);
          tabLayout.setupWithViewPager(tabsPager);
  
          tabsPager.setAdapter(tabsAdapter);
  
  
  
  
      }
  
      public void addedToCart(int price){
          priceCount = priceCount + price;
          cartCount = cartCount + 1;
  
          if (cartCount &gt; 0 &amp;&amp; priceCount &gt; 0){
              if (cartCount == 1){
                  itemsCounterView.setText( String.valueOf(cartCount) + " item");
              } else {
                  itemsCounterView.setText( String.valueOf(cartCount) + " items");
              }
              priceCounterView.setText(String.valueOf(priceCount) + " RWF");
  
              cartButton.setVisibility(View.VISIBLE);
          } else {
              cartButton.setVisibility(View.GONE);
          }
      }
  
      @Override
      public boolean onSupportNavigateUp() {
          onBackPressed();
          return true;
      }
  }
  </code></pre>
  
  <p><strong>PagerAdapter</strong></p>
  
  <pre><code>public class MenuPagerAdapter extends FragmentStatePagerAdapter {
  
      private List&lt;MenuType&gt; menuTypes;
  
      public MenuPagerAdapter(FragmentManager fm, List&lt;MenuType&gt; menuTypes) {
          super(fm);
          this.menuTypes = menuTypes;
      }
  
      @Override
      public Fragment getItem(int position) {
          return MenuFragment.newInstance(menuTypes.get(position));
      }
  
      @Override
      public int getCount() {
          return menuTypes.size();
      }
  
      @Nullable
      @Override
      public CharSequence getPageTitle(int position) {
  
          return menuTypes.get(position).getRmenu();
      }
  }
  </code></pre>
  
  <p><strong>Fragment</strong></p>
  
  <pre><code>public class MenuFragment extends Fragment {
  
      @BindView(R.id.menuRecyclerView) RecyclerView menuRecyclerView;
      @BindView(R.id.avi) AVLoadingIndicatorView avi;
      private MenuType menuType;
      private Bundle args;
      private MenuAdapter adapter;
      private Context context;
      private int position;
  
      public MenuFragment() {
          // Required empty public constructor
      }
  
      public static MenuFragment newInstance(MenuType mMenuType){
          MenuFragment fragment = new MenuFragment();
          Bundle args = new Bundle();
          args.putSerializable("data", mMenuType);
          fragment.setArguments(args);
          return fragment;
      }
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
  
          View view = inflater.inflate(R.layout.fragment_menu, container, false);
          ButterKnife.bind(this, view);
          context = container.getContext();
  
          menuRecyclerView.setHasFixedSize(true);
          menuRecyclerView.setLayoutManager(new LinearLayoutManager(context));
  
          adapter = new MenuAdapter(menuType.getMenuItems(), context);
          menuRecyclerView.setAdapter(adapter);
          avi.setVisibility(View.GONE);
  
          setData();
  
          return view;
      }
  
  
      @Override
      public void onCreate(@Nullable Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setRetainInstance(true);
          args = getArguments();
          menuType = (MenuType) args.getSerializable("data");
      }
  
      public MenuType getMenuType() {
          return menuType;
      }
  
      public void setMenuType(MenuType menuType) {
          this.menuType = menuType;
      }
  
  
      public void setData(){
          adapter = new MenuAdapter(menuType.getMenuItems(), getActivity());
          menuRecyclerView.setAdapter(adapter);
          avi.setVisibility(View.GONE);
      }
  }
  </code></pre>
  
  <p>As it is seen, I'm loading MenuTypes(List) first, then for each MenuType, I load all associated items (MenuItems) List too. I, then, create Tabs with respect to the number of <strong>MenuTypes</strong>, and then create instances of the <strong>MenuFragment</strong> with respect to the number of MenuItems in the list, and the for each instance of MenuFragment, I just list <strong>MenuItems</strong> for particular MenuType in a RecyclerView(which is working fine). I tried to add a static TextView in the MenuFragment, and it was shown every time, on all tabs, but the actual data I want are not shown. I also tried a temporary solution to manually set Data I want to each created fragment from the Activity in <strong>OnPageChangeListener</strong>, but it was not directly working on the first tab until I first navigate to any other tab, which made me to opt out the strategy. Any help is much appreciated. Screenshots are also included for reference.</p>
  
  <p><a href="https://i.stack.imgur.com/sZ4Uo.png" rel="nofollow noreferrer">When tabs are 2 (Screenshot)</a></p>
  
  <p><a href="https://i.stack.imgur.com/R3rAP.png" rel="nofollow noreferrer">When tabs are 2 (Screenshot)</a></p>
  
  <p><a href="https://i.stack.imgur.com/5b7WN.png" rel="nofollow noreferrer">3 tabs, at first, the tab 1 is empty (Screenshot)</a></p>
  
  <p><a href="https://i.stack.imgur.com/KRLfc.png" rel="nofollow noreferrer">3 tabs, last tab has data (Screenshot)</a></p>
  
  <p><a href="https://i.stack.imgur.com/4ojZV.png" rel="nofollow noreferrer">3 tabs, After navigating tabs, the tab 1 also got data (Screenshot)</a></p>
  
  <p><a href="https://i.stack.imgur.com/xRHlK.png" rel="nofollow noreferrer">3 tabs, tab 2 never gets data (Screenshot)</a></p>