title: Fragments being inflated with old data, when going back to an activity that was stopped
body: <p>Activity A has fragments. When it starts an intent to activity B, then when <code>B.finish()</code>, A executes <code>onCreate()</code> again.</p>
  
  <p>But this time, even though <code>A.onCreate()</code> has a new <code>PacksPagerAdapter</code> and a new <code>ViewPager</code>, the fragments are shown with old data.</p>
  
  <p>I can see that that <code>onCreateView()</code> is executed for each fragment, but it still has the old arguments since the <code>static newInstance()</code> wasn't called. Arguments are created when FragmentPagerAdapter's <code>getItem(position)</code> is called.</p>
  
  <p>Here's how it's implemented -</p>
  
  <pre><code>public class PackActivity extends Activity {
      ...
      PacksPagerAdapter mPacksPagerAdapter;
      ViewPager mViewPager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          mPacksPagerAdapter = new MyPagerAdapter(getFragmentManager());
  
          // Set up the ViewPager with the sections adapter.
          mViewPager = (ViewPager) findViewById(R.id.pager);
          mViewPager.setAdapter(mPacksPagerAdapter);
          mViewPager.setOffscreenPageLimit(PACK_PAGES - 1);
  
          mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
                      @Override
                      public void onPageSelected(int position) {
                          actionBar.setTitle(...);
                      }
                  });
  
      }
      ...
  }
  
  public class MyPagerAdapter extends FragmentPagerAdapter {
  ...
      @Override
      public Fragment getItem(int position) {
          // getItem is called to instantiate the fragment for the given page.
          // Return a PlaceholderFragment (defined as a static inner class
          // below).
          return PackFragment.newInstance(myData);
      }
  ...
  }
  
  public class PackFragment extends Fragment {
  ...
      public static PackFragment newInstance(PackInfo pack) {
          PackFragment fragment = new PackFragment();
  
          Bundle bdl = new Bundle(2);
          bdl.putSerializable(EXTRA_PACK, pack);
          bdl.putInt(EXTRA_PACK_POSITION, pack.packNumber);
          fragment.setArguments(bdl);
  
          return fragment;
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View packView = inflater.inflate(R.layout.pack_fragment, container, false);
  
          // this has the old argument, since newInstance(...) wasn't called      
          PackInfo pack = (PackInfo) getArguments().getSerializable(EXTRA_PACK);
          ...
  
          return packView;
      }
  
  ...
  }
  </code></pre>
  
  <p>Any idea why new fragments aren't being instantiated on the 2nd creation of activity A?</p>
  
  <h2>Update - Activity-Fragments lifecycle</h2>
  
  <p>The answer is, as Tal said, is that activity is being restored, so old fragments are being reattached instead of creating new ones.</p>
  
  <p>After spending a lot of time studying this, I've found that there are typically 3 scenarios for activity-fragments lifecycle. Here are the scenarios, the lifecycle and how to reload fragment's old data:</p>
  
  <p><strong>New activity</strong></p>
  
  <p><em>Lifecycle</em>: onCreate() ==> onResume() ==> Fragments are created and attached</p>
  
  <p>No need to reload.</p>
  
  <p><strong>Restored activity</strong></p>
  
  <p><em>Lifecycle</em>: onCreate() ==> Fragments inflated with old data and reattached ==> onResume()</p>
  
  <p>In onResume(), remove all fragments and create new ones, either manually or automatically using an adapter -</p>
  
  <pre><code>        // Remove all fragments
          FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
          for (int i = BuildConfig.PACK_PAGES - 1; i &gt;= 0; i--) {
              Fragment f = findFragmentByPosition(i);
              if (f != null)
                  fragmentTransaction.remove(f);
          }
          fragmentTransaction.commit();
  
          // This will force recreation of all fragments
          viewPager.setAdapter(packsPagerAdapter);
  </code></pre>
  
  <p><strong>Resumed activity</strong></p>
  
  <p><em>Lifecycle</em>: onResume()</p>
  
  <p>Same as activity restore, remove old fragments and recreate.</p>
  
  <p><strong>Note:</strong> Some OS versions always restores activity, even when opening a SettingsActivity for a few seconds, and other (older) versions will always resume.</p>