title: Viewpager displaying the required details only once and not displaying it a second time when coming back from main screen
body: <p>I am calling ViewPager(Fragment C) from a ListView(Fragment A). But this only works once. If I move back to my list and select another item , it doesnot move into the listview. It just shows a blank layout of fragment c.</p>
  
  <p>Below is the code for Fragment A: </p>
  
  <pre><code>public class FragmentA extends Fragment implements AdapterView.OnItemClickListener{
  
  ListView list;
  Communicator communicator;
  ArrayList&lt;Book&gt; book_a;
  ArrayList&lt;String&gt;  book_titles;
  
  
  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view= inflater.inflate(R.layout.fragment_a,container,false);
      savedInstanceState = getArguments();
      if (getArguments() != null) {
          // savedInstanceState = getArguments();
          book_a = (ArrayList&lt;Book&gt;) getArguments().getSerializable("bookarray");
          book_titles = getList(book_a);
          //Log.d("Frag_a:Title",book_a.get(5).getTitle());
          list= (ListView) view.findViewById(R.id.listview);
          ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(getActivity(),android.R.layout.simple_list_item_1,book_titles);
          list.setAdapter(adapter);
          list.setOnItemClickListener(this);
      }
      return view;
  }
  
  
  public ArrayList&lt;String&gt; getList(ArrayList&lt;Book&gt; book2){
  
      ArrayList&lt;String&gt; list_titles = new ArrayList&lt;String&gt;();
      int size = book2.size();
      for(int i=0;i&lt;size;i++){
          Book object;
          object = book2.get(i);
          list_titles.add(object.title);
      }
  
  
      return list_titles;
  }
  
  public void setCommunicator(Communicator communicator)
  {
      this.communicator = communicator;
  }
  
  @Override
  public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
      communicator.respond(position);
  
  }
  
  public interface Communicator{
      public void respond(int index);
  }
  
  @Override
  public void onAttach(Context context) {
      super.onAttach(context);
      if(context instanceof Communicator){
          communicator = (Communicator) context;
      } else {
          throw new RuntimeException(context.toString()+"must implement Communicator");
      }
  
  }
  
  @Override
  public void onDetach() {
      super.onDetach();
      communicator = null;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Below is the code for MainActivity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements FragmentA.Communicator{
  
  FragmentB f2;
  ArrayList&lt;Book&gt; b = new ArrayList&lt;Book&gt;();
  FragmentManager manager;
  static int flag = 0;
  static String search="great";
  SearchView sv ;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      sv = (SearchView) findViewById(R.id.searchView);
      sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
          @Override
          public boolean onQueryTextSubmit(String query) {
              flag = 1;
              search = query;
              getBooks();
              return false;
          }
  
          @Override
          public boolean onQueryTextChange(String newText) {
              getBooks();
              return false;
          }
      });
  
      getBooks();
      /*
      manager = getSupportFragmentManager();
      f1 = (FragmentA) manager.findFragmentById(R.id.fragment);
      f1.setCommunicator(this);*/
  
  }
  
  
  @Override
  public void respond(int index) {
      f2 = (FragmentB) manager.findFragmentById(R.id.fragment2);
      if(f2!=null &amp;&amp; f2.isVisible())
      {
          f2.changeData(index);
      }
      else
      {
          Bundle bundle = new Bundle();
          bundle.putInt("index", index);
          bundle.putSerializable("bookarray",(ArrayList&lt;Book&gt;)b);
          Fragment newFragment = new FragmentC();
          newFragment.setArguments(bundle);
          FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
          transaction.replace(R.id.fragment, newFragment);
          transaction.addToBackStack(null);
          transaction.commit();
  
  
      }
  }
  
  public void afterGetBooks(ArrayList&lt;Book&gt; bks) {
      for (Book h : bks) {
          b.add(h);
      }
      manager = getSupportFragmentManager();
      Bundle args = new Bundle();
      args.putSerializable("bookarray",(ArrayList&lt;Book&gt;)bks);
      FragmentA f1 = new FragmentA();
      f1.setArguments(args);
      FragmentTransaction transaction = manager.beginTransaction();
      transaction.replace(R.id.fragment,f1);
      transaction.addToBackStack(null);
      transaction.commit();
      f1.setCommunicator(this);
  
  
  }
  
  private void getBooks(){
  
      String url;
      if(flag==0){
      url = "https://kamorris.com/lab/audlib/booksearch.php/";}
      else{
          url = "https://kamorris.com/lab/audlib/booksearch.php?search=" + search;
          flag=0;
      }
      //ArrayList&lt;Book&gt; boo;
  
      Retrofit retrofit =  new Retrofit.Builder().baseUrl("https://kamorris.com/lab/audlib/booksearch.php/").addConverterFactory(GsonConverterFactory.create()).build();
  
      Book.API api = retrofit.create(Book.API.class);
  
      Call&lt;ArrayList&lt;Book&gt;&gt; call = api.getBooks(url);
      call.enqueue(new Callback&lt;ArrayList&lt;Book&gt;&gt;() {
          @Override
          public void onResponse(Call&lt;ArrayList&lt;Book&gt;&gt; call, Response&lt;ArrayList&lt;Book&gt;&gt; response) {
  
              ArrayList&lt;Book&gt; Books = response.body();
              for(Book h: Books){
                  Log.d("Retro-Title",h.getTitle());
                  //b.add(h);
              }
              afterGetBooks(Books);
          }
  
          @Override
          public void onFailure(Call&lt;ArrayList&lt;Book&gt;&gt; call, Throwable t) {
              Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
          }
      });
  
  
  }
  </code></pre>
  
  <p>Below is the code for Fragment C:</p>
  
  <pre><code>public class FragmentC extends Fragment {
  
  ViewPager vp;
  static int list_pos;
  FragmentPagerAdapter adapterViewPager;
  public static ArrayList&lt;Book&gt; book_c;
  
  @Nullable
  @Override
  public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
      View view = inflater.inflate(R.layout.fragment_c,container,false);
      if(getArguments()!= null){
  
      book_c  = (ArrayList&lt;Book&gt;) getArguments().getSerializable("bookarray");
      list_pos = getArguments().getInt("index");
  
      }
      vp = (ViewPager) view.findViewById(R.id.viewpager);
      adapterViewPager = new MyPagerAdapter(getFragmentManager());
      vp.setAdapter(adapterViewPager);
  
      return view;
  }
  
  public static class MyPagerAdapter extends FragmentPagerAdapter {
      private static int NUM_ITEMS = 11;
  
      public MyPagerAdapter(FragmentManager fragmentManager) {
          super(fragmentManager);
      }
  
      // Returns total number of pages
      @Override
      public int getCount() {
          return NUM_ITEMS;
      }
  
      // Returns the fragment to display for that page
      @Override
      public Fragment getItem(int position) {
          switch (list_pos) {
              case 0: // Fragment # 0 - This will show FirstFragment
                  list_pos = position;
                  return FragmentB.newInstance(book_c,0);
              case 1: // Fragment # 0 - This will show FirstFragment different title
                  list_pos = position;
                  return FragmentB.newInstance(book_c,1);
              case 2: // Fragment # 1 - This will show SecondFragment
                  list_pos = position;
                  return FragmentB.newInstance(book_c,2);
              case 3: list_pos = position;
                  return FragmentB.newInstance(book_c,3);
              case 4:list_pos = position;
                  return FragmentB.newInstance(book_c,4);
              case 5:list_pos = position;
                  return FragmentB.newInstance(book_c,5);
              case 6:list_pos = position;
                  return FragmentB.newInstance(book_c,6);
              case 7:list_pos = position;
                  return FragmentB.newInstance(book_c,7);
              case 8:list_pos = position;
                  return FragmentB.newInstance(book_c,8);
              case 9:list_pos = position;
                  return FragmentB.newInstance(book_c,9);
              default:
                  return null;
          }
      }
  
      // Returns the page title for the top indicator
      @Override
      public CharSequence getPageTitle(int position) {
          return "Page " + position;
      }
  
  }
  
  }
  </code></pre>
  
  <p>Also is there a way, where I can start my viewpager from the description of the listitem which I selected.</p>
  
  <p>Also Fragment B is just a text view holder.</p>
  
  <p>I tried to see the control flow with the debugger. But when I try it that way these lines in Fragment C are not working ..</p>
  
  <pre><code>    {
      book_c  = (ArrayList&lt;Book&gt;) getArguments().getSerializable("bookarray");
      list_pos = getArguments().getInt("index");
      }
      vp = (ViewPager) view.findViewById(R.id.viewpager);
      adapterViewPager = new MyPagerAdapter(getFragmentManager());
      vp.setAdapter(adapterViewPager);
  </code></pre>