title: RecyclerView in fragment through activity get position
body: <p>so here is my problem, i have only one activity from i launch my viewpager fragment, in my viewpager i have 4 fragments containing recyclerViews. Every Fragment contains item populated by data on the net, when i click into one i get a WebView Fragment, but when i click on the backbutton i lost my poisiton, cause it's recreating the whole activity.</p>
  
  <p>I don't know how to not recreate the activity and get back my old position, or recreating it but not changing the item's positions and have my old position back. Here's my code. Where i should put what ?!  Some help would be very very well come. 
  Thank You !!</p>
  
  <p><strong>My Activity Class :</strong> </p>
  
  <pre><code>public class LauncherActivity extends AppCompatActivity implements ListFragment.OnNewSelectedInterface{
  
      public static final String VIEWPAGER_FRAGMENT = "view_pager";
      private ImageView mImageView;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_launcher);
  
          RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.activity_launcher);
          mImageView = (ImageView) findViewById(R.id.logoImageView);
  
          mImageView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.rotate_indefinitely));
  
          if(!isNetworkAvailable()){
  
              alertUserAboutError();
  
          } else {
              relativeLayout.postDelayed(new Runnable() {
                  @Override
                  public void run() {
  
                      ViewPagerFragment savedFragment = (ViewPagerFragment)
                              getSupportFragmentManager().findFragmentByTag(VIEWPAGER_FRAGMENT);
  
                      if(savedFragment == null) {
                          ViewPagerFragment viewPagerFragment = new ViewPagerFragment();
                          FragmentManager fragmentManager = getSupportFragmentManager();
                          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                          fragmentTransaction.add(R.id.activity_launcher, viewPagerFragment, VIEWPAGER_FRAGMENT);
                          fragmentTransaction.addToBackStack(null);
                          fragmentTransaction.commit();
                      }
                  }
              }, 2000);
          }
      }
  
      @Override
      public void onListNewSelected(int index, ArrayList&lt;Articles&gt; articles) {
          WebViewFragment webViewFragment = new WebViewFragment();
          Bundle bundle = new Bundle();
          bundle.putInt(ViewPagerFragment.KEY_NEW_INDEX, index);
          bundle.putParcelableArrayList(ListFragment.KEY_LIST, articles);
          webViewFragment.setArguments(bundle);
          FragmentManager fragmentManager = getSupportFragmentManager();
          FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          fragmentTransaction.replace(R.id.activity_launcher, webViewFragment, WebViewFragment.WEBVIEWFRAGMENT);
          fragmentTransaction.addToBackStack(null);
          fragmentTransaction.commit();
      }
  
      private boolean isNetworkAvailable() {
          ConnectivityManager manager = (ConnectivityManager)
                  getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = manager.getActiveNetworkInfo();
          boolean isAvailable = false;
          if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {
              isAvailable = true;
          }
          return isAvailable;
      }
  
      private void alertUserAboutError() {
          AlertDialogFragment alertDialogFragment = new AlertDialogFragment();
          alertDialogFragment.show(getFragmentManager(), "error_dialog");
      }
  
  }
  </code></pre>
  
  <p><strong>My ViewPagerClass :</strong> </p>
  
  <pre><code>public class ViewPagerFragment extends Fragment {
  
      public static final String KEY_NEW_INDEX = "key_new_index";
      private ViewPager mViewPager;
      private TabLayout mTabLayout;
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  
          final View view = inflater.inflate(R.layout.fragment_view_pager, container, false);
  
          final OldFactFragment oldFactFragment = new OldFactFragment();
  
          final SportFragment sportFragment = new SportFragment();
  
          final BuzzFragment buzzFragment = new BuzzFragment();
  
          final NewsFragment newsFragment = new NewsFragment();
  
          mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
          mTabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
          mViewPager.setOffscreenPageLimit(4);
  
  
          mViewPager.setAdapter(new FragmentPagerAdapter(getChildFragmentManager()) {
              @Override
              public Fragment getItem(int position) {
                  if(position == 0){
                      return oldFactFragment;
                  } else if (position == 1) {
                      return newsFragment;
                  } else if (position == 2){
                      return sportFragment;
                  } else return buzzFragment;
              }
  
              @Override
              public CharSequence getPageTitle(int position) {
                  if(position == 0){
                      return "Pastly";
                  } else if (position == 1) {
                      return "politic";
                  } else if (position == 2){
                      return "sport";
                  } else return "buzz";
              }
  
              @Override
              public int getCount() {
                  return 4;
              }
  
          });
  
          mTabLayout.setupWithViewPager(mViewPager);
  
          return view;
      }
  }
  </code></pre>
  
  <p><strong>My ListFragment(Parent's class of the 4 Fragments in the ViewPager) :</strong> </p>
  
  <pre><code>public abstract class ListFragment extends Fragment {
  
      public static final String KEY_LIST = "key_list";
  
      public interface OnNewSelectedInterface {
          void onListNewSelected(int index, ArrayList&lt;Articles&gt; articles);
      }
  
      protected static final String TAG = ListFragment.class.getSimpleName();
      protected ArrayList&lt;Articles&gt; mArticlesList;
      protected ItemAdapter mArticleAdapter;
      protected RecyclerView mRecyclerView;
      protected OnNewSelectedInterface mListener;
      protected RecyclerView.LayoutManager mManager;
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
          mListener = (OnNewSelectedInterface) getActivity();
          View view = inflater.inflate(R.layout.fragment_list, container, false);
          mRecyclerView = (RecyclerView) view.findViewById(R.id.recyclerview);
          getInfos(getUrl());
          return view;
      }
  
      protected abstract String[] getUrl();
  
      private void getInfos(String[] url) {
  
          if (isNetworkAvailable()) {
  
              OkHttpClient client = new OkHttpClient();
              for (int i = 0; i &lt; getUrl().length; i++) {
                  Request request = new Request.Builder().url(url[i]).build();
  
                  client.newCall(request).enqueue(new Callback() {
  
                      @Override
                      public void onResponse(Call call, Response response) throws IOException {
  
                          try {
                              String jsonData = response.body().string();
                              if (response.isSuccessful()) {
                                  Log.v(TAG, jsonData);
  
                                  getMultipleUrls(jsonData);
  
                                  getActivity().runOnUiThread(new Runnable() {
                                      @Override
                                      public void run() {
  
                                          getCurrentArticles(mArticlesList);
                                      }
                                  });
                              } else {
                                  alertUserAboutError();
                              }
                          } catch (IOException | JSONException e) {
                              e.printStackTrace();
                          }
                      }
  
                      @Override
                      public void onFailure(Call call, IOException e) {
                          getActivity().runOnUiThread(new Runnable() {
                              @Override
                              public void run() {
                                  alertUserAboutError();
                              }
                          });
                      }
                  });
              }
          } else{
              alertUserAboutError();
          }
  
      }
  
      private void getMultipleUrls(String jsonData) throws JSONException {
          if (mArticlesList == null) {
              mArticlesList = getArticleForecast(jsonData);
          } else {
              mArticlesList.addAll(getArticleForecast(jsonData));
          }
      }
  
      private void getCurrentArticles(ArrayList&lt;Articles&gt; articles) {
  
          mArticleAdapter = new ItemAdapter(getActivity(), articles, mListener);
  
          mRecyclerView.setAdapter(mArticleAdapter);
          mManager = new LinearLayoutManager(getActivity());
          mRecyclerView.setLayoutManager(mManager);
  
      }
  
      private boolean isNetworkAvailable() {
          ConnectivityManager manager = (ConnectivityManager) getActivity().
                  getSystemService(Context.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = manager.getActiveNetworkInfo();
          boolean isAvailable = false;
          if (networkInfo != null &amp;&amp; networkInfo.isConnected()) {
              isAvailable = true;
          }
          return isAvailable;
      }
  
      private void alertUserAboutError() {
          AlertDialogFragment alertDialogFragment = new AlertDialogFragment();
          alertDialogFragment.show(getActivity().getFragmentManager(), "error_dialog");
      }
  
      private ArrayList&lt;Articles&gt; getArticleForecast(String jsonData) throws JSONException {
          JSONObject forecast = new JSONObject(jsonData);
          JSONArray articles = forecast.getJSONArray("articles");
  
          ArrayList&lt;Articles&gt; listArticles = new ArrayList&lt;&gt;(articles.length());
  
          for (int i = 0; i &lt; articles.length(); i++) {
              JSONObject jsonArticle = articles.getJSONObject(i);
              Articles article = new Articles();
  
              String urlImage = jsonArticle.getString("urlToImage");
  
              article.setTitle(jsonArticle.getString("title"));
              article.setDescription(jsonArticle.getString("description"));
              article.setImageView(urlImage);
              article.setArticleUrl(jsonArticle.getString("url"));
  
              listArticles.add(i, article);
          }
  
          return listArticles;
      }
  }
  </code></pre>
  
  <p><strong>And my ItemAdapter (Adapter Class) :</strong></p>
  
  <pre><code>public class ItemAdapter extends RecyclerView.Adapter&lt;ItemAdapter.ArticleViewHolder&gt; {
  
      private final ListFragment.OnNewSelectedInterface mListener;
      protected ArrayList&lt;Articles&gt; mArticlesList;
      protected Context mContext;
      private int mPosition;
      private int lastPosition = -1;
  
  
      public ItemAdapter(Context contexts, ArrayList&lt;Articles&gt; mArticleLi, ListFragment.OnNewSelectedInterface listener){
          mContext = contexts;
          mArticlesList = mArticleLi;
          mListener = listener;
      }
  
      @Override
      public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View view = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.list_item_linear, parent, false);
  
          view.findViewById(R.id.textView).setSelected(true);
          ArticleViewHolder articleViewHolder = new ArticleViewHolder(view);
          articleViewHolder.setIsRecyclable(false);
  
          return articleViewHolder;
      }
  
      @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
      @Override
      public void onBindViewHolder(ArticleViewHolder holder, int position) {
          mPosition = position;
          holder.bindArticle(mArticlesList.get(position));
          setAnimation(holder.itemView, position);
          holder.mImageView.setClipToOutline(true);
  
  
      }
  
      private void setAnimation(View viewToAnimate, int position) {
          if (position &gt; lastPosition) {
              Animation animation = AnimationUtils.loadAnimation(viewToAnimate.getContext(), android.R.anim.slide_in_left);
              viewToAnimate.startAnimation(animation);
              lastPosition = position;
          }
      }
  
      @Override
      public int getItemCount() {
          return mArticlesList.size();
      }
  
      public class ArticleViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnLongClickListener{
  
          private TextView mTitle, mArticle;
          private ImageView mImageView;
  
          private ArticleViewHolder(View itemView) {
              super(itemView);
  
              mTitle = (TextView) itemView.findViewById(R.id.textView);
              mArticle = (TextView) itemView.findViewById(R.id.showTextView);
              mImageView = (ImageView) itemView.findViewById(R.id.imageView2);
  
              itemView.setOnClickListener(this);
              itemView.setOnLongClickListener(this);
          }
  
          private void bindArticle(Articles article) {
  
              mTitle.setText(article.getTitle());
              mArticle.setText(article.getDescription());
              mArticle.setVisibility(View.INVISIBLE);
              Glide.with(mContext).load(article.getImageView()).into(mImageView);
          }
  
  
          @Override
          public void onClick(View view) {
              mListener.onListNewSelected(getLayoutPosition(), mArticlesList);
          }
  
          @Override
          public boolean onLongClick(View view) {
  
              String desc = mArticlesList.get(getAdapterPosition()).getDescription();
              mArticle.setVisibility(View.VISIBLE);
              mArticle.setText(desc);
              mArticle.postDelayed(new Runnable() {
                  public void run() {
                      mArticle.setVisibility(View.INVISIBLE);
                  }
              }, 3000);
              return true;
          }
  
      }
  }
  </code></pre>