title: All the views get reinitilized when a tab is created or deleted
body: <p>I am creating an application implementing multiple tabs like chrome tabs.A tab will hold a child fragment and on adding new tabs the child fragment is recreated for new tabs. The issue I am facing is that in my child fragment I have dynamic grid views and list views. When I created a list or grids and then new tab has created the list and grids are being removed. also when a tab is deleted the same occurs.</p>
  
  <p>If already there are 2 tabs and I added a list, I am able to switch tabs without getting list or grids removed.
  I logged inside onActivityCreated and onStart to test and when I create 2nd  tab onstart is calling 2 times, when I add 3rd tab it is calling 3 times and like that!! </p>
  
  <p><strong>I need to keep the views in other tabs if I add a new tab or delete another tab</strong> .</p>
  
  <p>Why this is Happening:</p>
  
  <p>My ViewPageradapter is:</p>
  
  <pre><code>    public class ViewPagerAdapter extends FragmentStatePagerAdapter {
          private final ArrayList&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
          private final ArrayList&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();
          Context context;
          ViewPager viewPager;
          TabLayout tabLayout;
          int selectedTabPosition;
          private boolean doNotifyDataSetChangedOnce = false;
          public ViewPagerAdapter(FragmentManager manager, Context context, ViewPager viewPager,
                                  TabLayout tabLayout) {
              super(manager);
              this.context = context;
              this.viewPager = viewPager;
              this.tabLayout = tabLayout;
          }
  
          @Override
          public Fragment getItem(int position) {
              return mFragmentList.get(position);
          }
  
          @Override
          public int getCount() {
              if (doNotifyDataSetChangedOnce) {
                  doNotifyDataSetChangedOnce = false;
                  notifyDataSetChanged();
              }
              return mFragmentList.size();
          }
  
          public void addFrag(Fragment fragment, String title) {
              mFragmentList.add(fragment);
              mFragmentTitleList.add(title);
          }
  
          public void removeFrag(int position) {
              removeTab(position);
              Fragment fragment = mFragmentList.get(position);
              mFragmentList.remove(fragment);
              mFragmentTitleList.remove(position);
              //destroyFragmentView(viewPager, position, fragment);
              notifyDataSetChanged();
              if (getCount() &gt; 0) tabLayout.setupWithViewPager(viewPager);
              //viewPager.setCurrentItem(getCount() - 1);
              setupTabLayout();
          }
  
          public View getTabView(final int position) {
              View view = LayoutInflater.from(context).inflate(R.layout.custom_tab_item, null);
              TextView tabItemName = (TextView) view.findViewById(R.id.textViewTabItemName);
              ImageView tabItemAvatar =
                  (ImageView) view.findViewById(R.id.imageViewTabItemAvatar);
              ImageButton remove = (ImageButton) view.findViewById(R.id.imageButtonRemove);
              if(getCount() == 1){
                  remove.setVisibility(View.INVISIBLE);
              }
              else{
                  remove.setVisibility(View.VISIBLE);
              }
              remove.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                          Log.d("Remove", "Remove");
                          if(getCount() != 1){
                              removeFrag(position);
                          }
                          else{
                             // Toast.makeText("Atleast One Tab is requied")
                          }
  
                  }
              });
              tabItemName.setText(mFragmentTitleList.get(position));
              tabItemName.setTextColor(context.getResources().getColor(android.R.color.background_light));
              tabItemAvatar.setImageResource(R.drawable.boy);
              return view;
          }
          public void setupTabLayout() {
              selectedTabPosition = viewPager.getCurrentItem();
              for (int i = 0; i &lt; tabLayout.getTabCount(); i++) {
                  tabLayout.getTabAt(i).setCustomView(getTabView(i));
              }
          }
  
          public void removeTab(int position) {
              if (tabLayout.getChildCount() &gt; 0 &amp;&amp; tabLayout!=null) {
                  tabLayout.removeTabAt(position);
  
              }
  
          }
  
  
          @Override
          public int getItemPosition(Object object) {
                  return POSITION_NONE;
              }
          @Override
          public CharSequence getPageTitle(int position) {
              return mFragmentTitleList.get(position);
          }
      } 
  
  
  Parent Fragment:
   @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_parent, container, false);
          getIDs(view);
          setEvents();
          return view;
      }
  
      private void getIDs(View view) {
          viewPager = (ViewPager) view.findViewById(R.id.my_viewpager);
          tabLayout = (TabLayout) view.findViewById(R.id.my_tab_layout);
          add= (ImageView) view.findViewById(R.id.imageButtonAdd1);
          adapter = new ViewPagerAdapter(getFragmentManager(), getActivity(), viewPager, tabLayout);
          viewPager.setAdapter(adapter);
      }
  
      int selectedTabPosition;
  
      private void setEvents() {
          tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
              @Override
              public void onTabSelected(TabLayout.Tab tab) {
                  super.onTabSelected(tab);
                  viewPager.setCurrentItem(tab.getPosition());
                  selectedTabPosition = viewPager.getCurrentItem();
                  Toast.makeText(getActivity(), "Selected " + tab.getPosition(), Toast.LENGTH_SHORT).show();
                  Log.d("Selected", "Selected " + tab.getPosition());
              }
  
              @Override
              public void onTabUnselected(TabLayout.Tab tab) {
                  super.onTabUnselected(tab);
                  Log.d("Unselected", "Unselected " + tab.getPosition());
              }
          });
  
          add.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if(adapter.getCount() &gt; 4){
                      Toast.makeText(getActivity(), "Only 5 Tabs allowed", Toast.LENGTH_SHORT).show();
                  }
                  else {
                      Bundle bundle = new Bundle();
                      bundle.putString("data", "POS"+adapter.getCount());
                      FragmentChild fragmentChild = new FragmentChild();
                      fragmentChild.setArguments(bundle);
                      adapter.addFrag(fragmentChild, "POS"+adapter.getCount());
                      adapter.notifyDataSetChanged();
                      if (adapter.getCount() &gt; 0) tabLayout.setupWithViewPager(viewPager);
  
                      viewPager.setCurrentItem(adapter.getCount() - 1);
                      setupTabLayout();
                  }
              }
          });
      }
  
      public void addPage(String pagename) {
          Bundle bundle = new Bundle();
          bundle.putString("data", pagename);
          FragmentChild fragmentChild = new FragmentChild();
          fragmentChild.setArguments(bundle);
          adapter.addFrag(fragmentChild, pagename);
          adapter.notifyDataSetChanged();
          if (adapter.getCount() &gt; 0) tabLayout.setupWithViewPager(viewPager);
  
          viewPager.setCurrentItem(adapter.getCount() - 1);
          setupTabLayout();
      }
  
      public void setupTabLayout() {
          selectedTabPosition = viewPager.getCurrentItem();
          for (int i = 0; i &lt; tabLayout.getTabCount(); i++) {
              tabLayout.getTabAt(i).setCustomView(adapter.getTabView(i));
          }
      }
  </code></pre>
  
  <p>CHILD FRAGMENT WHERE MY LIST EXIST IS:</p>
  
  <pre><code>public class FragmentChild extends Fragment {
      String childname;
      TextView textViewChildName;
      EditText editText;
      private GridView mGridView;
      private ListItem mListItem;
      private ListView mListview;
      private ProgressBar mProgressBar;
      private ProductViewAdapter mGridAdapter;
      private ListViewAdapter mListAdapter = null;
      private ArrayList&lt;GridItem&gt; mGridData;
      private ArrayList&lt;ListItem&gt; mListData = null;
      ListView listView;
      CheckInterNetConnection check ;
      Boolean isInternetPresent = false;
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_child, container, false);
          Bundle bundle = getArguments();
          childname = bundle.getString("data");
          getIDs(view);
          setEvents();
          return view;
      }
  
      private void getIDs(View view) {
          //textViewChildName = (TextView) view.findViewById(R.id.textViewChild);
          //textViewChildName.setText(childname);
          //editText = (EditText) view.findViewById(R.id.editText);
          //editText.setText("");
      }
  
      private void setEvents() {
  
      }
  
      @Override
      public void onActivityCreated(Bundle savedInstanceState) {
          super.onActivityCreated(savedInstanceState);
          Log.e("onActivityCreated","onActivityCreated");
          mGridView = (GridView) getView().findViewById(R.id.gridView);
          mGridData = new ArrayList&lt;&gt;();
          mGridAdapter = new ProductViewAdapter(getActivity(), R.layout.grid_product_layout, mGridData);
          mGridView.setAdapter(mGridAdapter);
  
          mListview = (ListView) getView().findViewById(R.id.list);
          mListData = new ArrayList&lt;&gt;();
          mListAdapter = new ListViewAdapter(getActivity(), R.layout.list_row, mListData);
          mListview.setAdapter(mListAdapter);
  
      }
      @Override
      public void onStart() {
          super.onStart();
          Log.e("onSTart","onSTart");
          check = new CheckInterNetConnection(getActivity());
          isInternetPresent = check.isConnectingToInternet();
          if (isInternetPresent) {
              //fetchProducts();
          }
      }
      public void fetchProducts(){
          Log.e("URL","http://13.126.17.194/products.php");
          RestClientHelper.getInstance().get("http://13.126.17.194/products.php", new RestClientHelper.RestClientListener() {
              @Override
              public void onSuccess(String response) {
                  parseResult(response);
                  mListData.clear();
                  mListAdapter.setGridData(mListData);
                  mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                      public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) {
                          try {
                              ListItem items;
                              GridItem item = (GridItem) parent.getItemAtPosition(position);
                              items = new ListItem();
                              items.setName(item.getTitle());
                              items.setType(item.getDescription());
                              mListData.add(items);
                          }
                          catch(Exception e){
  
                          }
                          finally {
                              mListAdapter.setGridData(mListData);
                          }
                      }
                  });
              }
  
              @Override
              public void onError(String error) {
  
              }
          });
      }
  
      private void parseResult(String result) {
          try {
              JSONObject response = new JSONObject(result);
              JSONArray posts = response.optJSONArray("products");
              GridItem item;
              if(posts.length() &lt;= 0){
                  RelativeLayout ly = (RelativeLayout) getView().findViewById(R.id.noOps);
                  ly.setVisibility(View.VISIBLE);
              }
              else {
                 // RelativeLayout ly = (RelativeLayout)  getView().findViewById(R.id.noOps);
                  //ly.setVisibility(View.INVISIBLE);
                  for (int i = 0; i &lt; posts.length(); i++) {
                      JSONObject post = posts.optJSONObject(i);
                      String id = post.optString("id");
                      String title = post.optString("name");
                      String description = post.optString("description");
                      String image = post.optString("image");
                      String qty = post.optString("qty");
                      String quantityin = post.optString("quantityin");
                      String price = post.optString("price");
  
                      item = new GridItem();
                      item.setId(id);
                      item.setTitle(title);
                      item.setDescription(description);
                      item.setImage(image);
                      item.setQuantity(qty);
                      item.setQuantityIn(quantityin);
                      item.setUnitprice(price);
                      mGridData.add(item);
                  }
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
          finally {
              mGridAdapter.setGridData(mGridData);
          }
      }
  }
  </code></pre>
  
  <p>PARENT FRAGMENT:</p>
  
  <pre><code>public class FragmentParent extends Fragment {
      private TabLayout tabLayout;
      private ViewPager viewPager;
      private ViewPagerAdapter adapter;
      private ImageView add;
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          View view = inflater.inflate(R.layout.fragment_parent, container, false);
          getIDs(view);
          setEvents();
          return view;
      }
  
      private void getIDs(View view) {
          viewPager = (ViewPager) view.findViewById(R.id.my_viewpager);
          tabLayout = (TabLayout) view.findViewById(R.id.my_tab_layout);
          add= (ImageView) view.findViewById(R.id.imageButtonAdd1);
          adapter = new ViewPagerAdapter(getFragmentManager(), getActivity(), viewPager, tabLayout);
          viewPager.setAdapter(adapter);
      }
  
      int selectedTabPosition;
  
      private void setEvents() {
          tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {
              @Override
              public void onTabSelected(TabLayout.Tab tab) {
                  super.onTabSelected(tab);
                  viewPager.setCurrentItem(tab.getPosition());
                  selectedTabPosition = viewPager.getCurrentItem();
                  Toast.makeText(getActivity(), "Selected " + tab.getPosition(), Toast.LENGTH_SHORT).show();
                  Log.d("Selected", "Selected " + tab.getPosition());
              }
  
              @Override
              public void onTabUnselected(TabLayout.Tab tab) {
                  super.onTabUnselected(tab);
                  Log.d("Unselected", "Unselected " + tab.getPosition());
              }
          });
  
          add.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  if(adapter.getCount() &gt; 4){
                      Toast.makeText(getActivity(), "Only 5 Tabs allowed", Toast.LENGTH_SHORT).show();
                  }
                  else {
                      Bundle bundle = new Bundle();
                      bundle.putString("data", "POS"+adapter.getCount());
                      FragmentChild fragmentChild = new FragmentChild();
                      fragmentChild.setArguments(bundle);
                      adapter.addFrag(fragmentChild, "POS"+adapter.getCount());
                      adapter.notifyDataSetChanged();
                      if (adapter.getCount() &gt; 0) tabLayout.setupWithViewPager(viewPager);
  
                      viewPager.setCurrentItem(adapter.getCount() - 1);
                      setupTabLayout();
                  }
              }
          });
      }
  
      public void addPage(String pagename) {
          Bundle bundle = new Bundle();
          bundle.putString("data", pagename);
          FragmentChild fragmentChild = new FragmentChild();
          fragmentChild.setArguments(bundle);
          adapter.addFrag(fragmentChild, pagename);
          adapter.notifyDataSetChanged();
          if (adapter.getCount() &gt; 0) tabLayout.setupWithViewPager(viewPager);
  
          viewPager.setCurrentItem(adapter.getCount() - 1);
          //setupTabLayout();
      }
  
      public void setupTabLayout() {
          selectedTabPosition = viewPager.getCurrentItem();
          for (int i = 0; i &lt; tabLayout.getTabCount(); i++) {
              tabLayout.getTabAt(i).setCustomView(adapter.getTabView(i));
          }
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>I am able to solve the gridview updates multiple time but still, the views get reinitialized if I add or remove tabs. Please help</p>