Data List duplicate after go to other fragment (Sliding Tab)
<p>there are 3 tabs : <strong>ONE</strong> || <strong>TWO</strong> || <strong>TRI</strong></p>

<p>example :
<code>myList = 1,2,3</code></p>

<p><strong>issue :</strong></p>

<p>after i go to page <strong>TWO</strong>
<code>myList = 1,2,3,1,2,3</code> (double duplicate)</p>

<p>if i go to page <strong>TRI</strong>
<code>myList = 1,2,3,1,2,3,1,2,3</code> (triple duplicate)</p>

<p>after i looking for the solution on internet, i found this code (on Adapter class) :</p>

<pre><code>   public void swap(List&lt;FoodModel&gt; datas){
        datas = new ArrayList&lt;&gt;();//updated
        if(mListFood !=null || mListFood.size() !=0){
            mListFood.clear();
            mListFood.addAll(datas);
        }else{
            mListFood = datas;
        }
        notifyDataSetChanged();
    }
</code></pre>

<p>i used like this in <code>oneFragment.java</code>:</p>

<pre><code>   mListFoodAdapter = new ListFoodAdapter(getContext(), mFoodModel);
        mListFoodAdapter.swap(mFoodModel);
        mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
        mRecyclerViewListFood.setAdapter(mListFoodAdapter);
        mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());
</code></pre>

<p>but it gave me <code>nullPointer</code> sometimes there is no data show in <code>RecyclerView</code></p>

<p>please, suggest me how to make the list data isn't duplicate anymore after back from page <strong>TWO</strong> of <strong>TRI</strong></p>

<p>bellow my codes <code>oneFragment.java</code> and <code>ViewPagerAdapter.java</code></p>

<p><strong>oneFragment.java</strong></p>

<pre><code>public class FoodFragment extends Fragment {

    private RecyclerView mRecyclerViewListFood;
    private List&lt;FoodModel&gt; mFoodModel = new ArrayList&lt;&gt;();
    private ListFoodAdapter mListFoodAdapter;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_foods, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        FragmentActivity fragmentActivity = getActivity();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(fragmentActivity);
        mRecyclerViewListFood = (RecyclerView) view.findViewById(R.id.recyclerView_list_foods);
        mFoodModel.add(new FoodModel("1",String.valueOf(R.drawable.icon), "test 1"));
        mFoodModel.add(new FoodModel("2",String.valueOf(R.drawable.icon), "test 2"));

        mListFoodAdapter = new ListFoodAdapter(getContext(), mFoodModel);
        mRecyclerViewListFood.setLayoutManager(linearLayoutManager);
        mRecyclerViewListFood.setAdapter(mListFoodAdapter);
        mRecyclerViewListFood.setItemAnimator(new DefaultItemAnimator());



    }
}
</code></pre>

<p><strong>ViewPagerAdapter.java :</strong></p>

<pre><code>public class ViewPagerDetailStandAdapter extends FragmentStatePagerAdapter {

    private final List&lt;Fragment&gt; mFragmentList = new ArrayList&lt;&gt;();
    private final List&lt;String&gt; mFragmentTitleList = new ArrayList&lt;&gt;();

    public ViewPagerDetailStandAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}
</code></pre>