title: Fragments not getting recreated by FragmentStatePagerAdapter after coming back to the main Fragment instance
body: <p><strong>SCOPE</strong></p>
  
  <p>I am making an application for my college fest which shows events for 4 particular fixed dates.
  I have a main_activity which has a linearlayout on which each and every fragment is displayed. Now a I have a Fragment "<em>ScheduleFragment</em>".</p>
  
  <p>ScheduleFragment consists of a ViewPager and a FragmentStatePagerAdapter which make it possible to toggle through 4 dates.</p>
  
  <p>The ViewPager has 4 pages representing 4 dates and these 4 pages come from 4 references of a same fragment i.e "<em>ScheduleListSubFragment</em>"</p>
  
  <p>From here a user selects an Event and can view the eventDescription which is contained in "<em>EventDescriptionFragment</em>"</p>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>The flow goes well. User come across ScheduleFragment, then toggles to the appropriate date and selects an event. </p>
  
  <p>Now when he goes back from the <em>EventDescriptionFragment</em> to <em>ScheduleFragment</em> (I will later tell how I am doing this), the ViewPager shows blank view. And when we swipe to the next date, the events for the next date comes, and we swipe back to previous date, the events for previous date are back. </p>
  
  <p>I debugged my application and came to know that when we go back to <em>ScheduleFragment</em>, then viewpager is not creating new fragment instances for <em>ScheduleListSubFragment</em> unless I scroll the viewpager.</p>
  
  <p>So, I guess ViewPager is enable to bring that particular reference (of <em>ScheduleListSubFragment</em>) which was being viewed by the user.</p>
  
  <p><em><strong>How am I going back to ScheduleFragment</em></strong>?
  I have created a stack in main_activity. When a user selects an event, I push the current reference of <em>ScheduleFragment</em> into the stack. And when back is pressed, I pop that reference and place it on the screen.</p>
  
  <p><strong>CODE</strong>
  *<strong><em>ScheduleFragment</em>*</strong>
              import com.Nit_kkr.updates.app.adapters.ScheduleFragmentAdapter;
              import com.viewpagerindicator.PageIndicator;
              import com.viewpagerindicator.TitlePageIndicator;
              import com.viewpagerindicator.TitlePageIndicator.IndicatorStyle;</p>
  
  <pre><code>        import android.os.Bundle;
          import android.support.v4.app.Fragment;
          import android.support.v4.view.ViewPager;
          import android.view.LayoutInflater;
          import android.view.View;
          import android.view.ViewGroup;
          import android.widget.TextView;
          import android.widget.Toast;
  
          public class ScheduleFragment extends Fragment{
              ScheduleFragmentAdapter mAdapter;
              ViewPager mPager;
              PageIndicator mIndicator;
              TitlePageIndicator indicator;
  
  
  
  
              @Override
              public void onViewStateRestored(Bundle savedInstanceState) {
          //      Toast.makeText(getActivity(), " onViewStateRestored()", Toast.LENGTH_SHORT).show();
                  super.onViewStateRestored(savedInstanceState);
              }
  
              @Override
              public void onCreate(Bundle savedInstanceState) {
                  super.onCreate(savedInstanceState);
                  mAdapter = new ScheduleFragmentAdapter(getActivity().getSupportFragmentManager());
                  mAdapter.notifyDataSetChanged();
              }
  
              @Override
              public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
                  View v = inflater.inflate(R.layout.scheduled_list, null);
                  prepareSlidingPager(v);
  
                  return v;
              }
  
              private void prepareSlidingPager(View v)
              {
  
                      mPager = (ViewPager)v.findViewById(R.id.pager);
  
                      mPager.setOffscreenPageLimit(4);
                      mPager.setAdapter(mAdapter);
  
                      mIndicator = (TitlePageIndicator)v.findViewById(R.id.indicator);
                      indicator = (TitlePageIndicator) mIndicator;
                      mIndicator.setViewPager(mPager);
  
                      final float density = getResources().getDisplayMetrics().density;
                      indicator.setBackgroundColor(0x18AF0000);
                      indicator.setFooterColor(0xFFAA2222);
  
                      //indicator.setFooterColor(0xFFAA33);
  
                      indicator.setFooterLineHeight(2 * density); //1dp
                      indicator.setFooterIndicatorHeight(3 * density); //3dp
                      indicator.setFooterIndicatorStyle(IndicatorStyle.Underline);
  
                      indicator.setTextColor(0xAA000000);
                      indicator.setSelectedColor(0xFF000000);
                      indicator.setSelectedBold(true);
              }
  
  
              public static ScheduleFragment newInstance()
              {
                  ScheduleFragment frag = new ScheduleFragment();
                  return frag;
              }
          }
  </code></pre>
  
  <p><strong>ScheduleListSubFragment:</strong></p>
  
  <pre><code>            public class ScheduleListSubFragment extends Fragment implements OnItemClickListener{
  
                  long startDate;
                  long endDate;
  
  
                  private ListView eventlist;
                  private listadapter adapter;
                  private List events;
                  private MyDB db;
  
  
  
                  @Override
                  public void onAttach(Activity activity) {
                      Toast.makeText(getActivity(), " onAttatch()------------------sublist schedule", Toast.LENGTH_SHORT).show();
                      super.onAttach(activity);
                  }
  
                  @Override
                  public void onCreate(Bundle savedInstanceState) {
                      super.onCreate(savedInstanceState);
  
                      db=new MyDB(this.getActivity());
  
                      Toast.makeText(getActivity(), " onCreate()------------------sublist schedule", Toast.LENGTH_SHORT).show();
  
                  }
  
                  @Override
                  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                          Bundle savedInstanceState) {
  
                      //Toast.makeText(getActivity(), " onCreateView()------------------sublist schedule", Toast.LENGTH_SHORT).show();
  
                      View v = inflater.inflate(R.layout.eventslist_subfragment_layout, null);
  
                      eventlist=(ListView)v.findViewById(R.id.Event_list);
                      eventlist.setOnItemClickListener(this);
  
                      fetcheventdata(getArguments().getLong("start_time"), getArguments().getLong("end_time"));
                      return v;
  
                  }
  
                  public static ScheduleListSubFragment newInstance(long starttime, long endtime)
                  {
                      ScheduleListSubFragment frag = new ScheduleListSubFragment();
                      Bundle bundle = new Bundle();
  
                      bundle.putLong("start_time", starttime);
                      bundle.putLong("end_time", endtime);
                      frag.setArguments(bundle);
  
                      return frag;
                  }
  
  
  
                   private void fetcheventdata(long starttime, long endtime)
                      {
  
                          db.open();
                          events=db.getEvents_bycategory_bytime(null, starttime, endtime);
                          db.close();
                          if(events!=null)
                          {
                          adapter=new listadapter(getActivity(), android.R.layout.simple_expandable_list_item_1, events);
                          eventlist.setAdapter(adapter);
  
                          adapter.notifyDataSetChanged();
                          }
                          else
                              Toast.makeText(getActivity(), "No Events Yet", Toast.LENGTH_LONG);
  
                          //Toast.makeText(getActivity(), events.size(), Toast.LENGTH_LONG).show();
  
  
  
                      }
  
  
                   private class listadapter extends ArrayAdapter&lt;Event&gt;
                      {
                          private List items;
  
                          public listadapter(Context context, int textViewResourceId,
                                  List objects){
                              super(context, textViewResourceId, objects);
                              items=new ArrayList();
                              items=objects;
  
                          }
  
                          @Override
                          public View getView(int position, View convertView, ViewGroup parent) {
  
                              View v=convertView;
  
                              if(v==null)
                              {
                                  LayoutInflater vi=(LayoutInflater)getActivity().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
                                  v=vi.inflate(R.layout.upcominglist, null);
  
                              }
  
                              Event event=(Event)items.get(position);
                              if(event!=null)
                              {
                                  TextView name=(TextView)v.findViewById(R.id.Name);
                                  TextView venue=(TextView)v.findViewById(R.id.Venue);
                                  TextView time=(TextView)v.findViewById(R.id.time);
  
  
                                  name.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
                                  venue.setTypeface(Utils.getHeroFace(getActivity().getAssets()));
  
                                  name.setTextColor(getResources().getColor(R.color.black));
                                  venue.setTextColor(getResources().getColor(R.color.darkgrey));
                                  time.setTextColor(getResources().getColor(R.color.black));
  
  
                                  //ImageView favouriteicon=(ImageView)v.findViewById(R.id.fav_status_image);
              //                  ImageView remindericon=(ImageView)v.findViewById(R.id.reminder_status_image);
  
                                  name.setText(event.getName());
                                  time.setText(Html.fromHtml("&lt;b&gt;"+event.getEvent_time()+"&lt;/b&gt;&lt;br&gt;&lt;/br&gt;"+event.getEvent_date()));
                                  venue.setText(event.getVenue());
  
  
                              }
                              return v;
                          }
  
                      }
  
  
                   @Override
                      public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1, int pos, long arg3) {
                          eventnotifierapp app=(eventnotifierapp)getActivity().getApplication();
                          app.crntEvent=(Event) events.get(pos);
                          //startActivity(new Intent(this.getActivity(), EventDescription.class));
  
                          EventDescriptionFragment frag = EventDescriptionFragment.NewInstance();
                          ((MainActivity)getActivity()).OpenNextFragment(frag);
  
                      }
  
              }
  </code></pre>
  
  <p><strong>ScheduleFragmentAdapter:</strong></p>
  
  <pre><code>    public class ScheduleFragmentAdapter extends FragmentStatePagerAdapter{
          String cat_name;
  
          protected static final String[] CONTENT = new String[] { "Thursday, Feb 20", 
              "Friday, Feb 21", "Saturday, Feb 22", "Sunday, Feb 23", };
  
          private int mCount = CONTENT.length;
  
          public ScheduleFragmentAdapter(FragmentManager fm) {
              super(fm);
  
  
          }
  
          @Override
          public Fragment getItem(int position) {
              if(position == 0)
                  return ScheduleListSubFragment.newInstance(day1start, day1end);
              if(position == 1)
                  return ScheduleListSubFragment.newInstance(day2start, day2end);
              if(position == 2)
                  return ScheduleListSubFragment.newInstance(day3start, day3end);
              else 
                  return ScheduleListSubFragment.newInstance(day4start, day4end);
          }
  
          @Override
          public int getCount() {
              return mCount;
          }
  
          @Override
          public CharSequence getPageTitle(int position) {
            return CONTENT[position % CONTENT.length];
          }
      }
  </code></pre>
  
  <p><strong>Research</strong>
  I did some digging on stackOverFlow, and found some related queries, which were not exactly the same, but couldnt arrive at a solution</p>
  
  <p>Any Kind of Help, is highly appreciated.</p>