title: Replacing Fragment in ViewPager shows blank screen?
body: <p>I'm having a lot of trouble getting Fragments to work with Tabs/Swiping. I have one activity and two Fragments, one which has a ListView. What I want is when I swipe from fragment 1 to the fragment with the ListView, to refresh the ListView or if possible recreate the second fragment....</p>
  
  <p>What I am trying right now is to use FragmentTransaction.replace() like this:</p>
  
  <pre><code>public void onTabSelected(Tab arg0, android.app.FragmentTransaction arg1) {
      // TODO Auto-generated method stub
  
      Fragment frag = new FragmentTwo();
  
      FragmentTransaction trans = getSupportFragmentManager().beginTransaction();
  
      if (arg0.getPosition() == 1) {
          trans.replace(R.id.pager, frag);
          trans.addToBackStack(null);
          trans.commit();
      }   
  }
  </code></pre>
  
  <p>So I thought that when I swipe to tab 2, which is the one with the ListView, it would replace it with a new one, which it creates. What is actually happening is that when I swipe to tab 2, it recreates the fragment with the ListView (I know because I have a dialog in the onCreateView which shows to the screen) but then the fragment/tab is blank/black. What am I doing wrong? Where does this fragment which is created go?</p>
  
  <p>I don't know if the code of the fragments is relevant but please let me know if I should post it and I can do that. This is my PagerAdapter class.</p>
  
  <pre><code>public class TabsPagerAdapter extends FragmentPagerAdapter {
  public TabsPagerAdapter(FragmentManager fm) {
      super(fm);
  }
  
  @Override
  public Fragment getItem(int index) {
  
      switch (index) {
      case 0:
          return new FragmentOne();
      case 1:
          return new FragmentTwo();
      case 2:
          return new FragmentThree();
      }
      return null;
  }
  
  @Override
  public int getCount() {
      // get item count - equal to number of tabs
      return 3;
  }
  }
  </code></pre>
  
  <p>Thanks for any help! I'm really pulling my hair out over this one :(</p>
  
  <h2>Edit:</h2>
  
  <p>When you swipe from tab 1 to tab 2, tab 2 shows my dialog and then loads the tab, but if I go back to tab 1 its all black...and if I then go to tab 2 again, it creates again and shows my dialog and then goes black as well...</p>
  
  <h2>Edit: (adding listview fragment code)</h2>
  
  <p>public class FragmentTwo extends Fragment {</p>
  
  <pre><code>// Progress Dialog
  private ProgressDialog pDialog;
  
  // Creating JSON Parser object
  JSONParser_Helpers2 jParser = new JSONParser_Helpers2();
  
  ArrayList&lt;HashMap&lt;String, String&gt;&gt; tripList;
  
  // url to get all products list
  private static String url_all_trips = "http://10.0.2.2/android_connect/get_all_trips.php";
  
  // JSON Node names
  private static final String TAG_SUCCESS = "success";
  private static final String TAG_TRIPS = "trips";
  private static final String TAG_TRIPID = "tripid";
  private static final String TAG_TRIPNAME = "tripName";
  private static final String TAG_UID = "uid32";
  
  // products JSONArray
  JSONArray trips = null;
  
  View rootView;
  Button btnRefresh;
  
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
          Bundle savedInstanceState) {
      //View rootView = inflater.inflate(R.layout.fragment_one, container, false);
  
      rootView = inflater.inflate(R.layout.fragment_two, container, false);
      btnRefresh = (Button) rootView.findViewById(R.id.refresh);
  
      // Hashmap for ListView
      tripList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
      // Loading products in Background Thread
      new LoadAllProducts().execute();
  
      // Get listview
      ListView lv = (ListView) rootView.findViewById(R.id.list);
  
  
      // on seleting single product
      // launching Edit Product Screen
      lv.setOnItemClickListener(new OnItemClickListener() {
  
      @Override
      public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position, long id) {
          //getting values from selected ListItem
          String tripid = ((TextView) view.findViewById(R.id.pid)).getText().toString();
  
          //starting new intent
          Intent in = new Intent(getActivity(), EditTrip_Activity.class);
          //sending pid to next activity
          in.putExtra(TAG_TRIPID, tripid);
  
          //starting new activity and expecting some response back
          startActivityForResult(in, 100);
      }
  
      });
      return rootView;
  }
  
  /**
   * Background Async Task to Load all product by making HTTP Request
   * */
  class LoadAllProducts extends AsyncTask&lt;String, String, String&gt; {
  
      ListView lv = (ListView) rootView.findViewById(R.id.list);
      /**
       * Before starting background thread Show Progress Dialog
       * */
      @Override
      protected void onPreExecute() {
          super.onPreExecute();
          pDialog = new ProgressDialog(getActivity());
          pDialog.setMessage("Loading your trips. Please wait...");
          pDialog.setIndeterminate(false);
          pDialog.setCancelable(false);
          pDialog.show();
      }
  
      /**
       * getting All products from url
       * */
      protected String doInBackground(String... args) {
          // Building Parameters
          List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
          // getting JSON string from URL
          JSONObject json = jParser.makeHttpRequest(url_all_trips, "GET", params);
  
          // Check your log cat for JSON response
          Log.d("All Trips: ", json.toString());
  
          try {
              // Checking for SUCCESS TAG
              int success = json.getInt(TAG_SUCCESS);
  
              if (success == 1) {
                  // products found
                  // Getting Array of Products
                  trips = json.getJSONArray(TAG_TRIPS);
  
                  // looping through All Products
                  for (int i = 0; i &lt; trips.length(); i++) {
                      JSONObject c = trips.getJSONObject(i);
  
                      // Storing each json item in variable
                      String tripid = c.getString(TAG_TRIPID);
                      String tripname = c.getString(TAG_TRIPNAME);
                      String userId = c.getString("uid");
  
                      // creating new HashMap
                      DatabaseHandler_Helpers db = new DatabaseHandler_Helpers(getActivity());
                      HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
  
                      if (userId.equals(db.getUserDetails().get("uid"))) {
                          // adding each child node to HashMap key =&gt; value
                          map.put(TAG_TRIPID, tripid);
                          map.put(TAG_TRIPNAME, tripname);
  
                          // adding HashList to ArrayList
                          tripList.add(map);
                      } //else {
                          //map.put(TAG_TRIPID, "");
                          //map.put(TAG_TRIPNAME, "You have no tracked trips.");
  
                          //tripList.add(map);
                      //}
                  }
              } else {
                  // no products found
                  // Launch Add New product Activity
                  Intent i = new Intent(getActivity(),
                          NewTrip_Activity.class);
                  // Closing all previous activities
                  i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  startActivity(i);
              }
          } catch (JSONException e) {
              e.printStackTrace();
          }
  
          return null;
      }
  
      /**
       * After completing background task Dismiss the progress dialog
       * **/
      protected void onPostExecute(String file_url) {
          // dismiss the dialog after getting all products
          pDialog.dismiss();
  
          // updating UI from Background Thread
          ((Activity) getActivity()).runOnUiThread(new Runnable() {
              public void run() {
                  /**
                   * Updating parsed JSON data into ListView
                   * */
  
                  ListAdapter adapter = new SimpleAdapter(
                          getActivity(), tripList,
                          R.layout.list_item, new String[] { TAG_TRIPID,
                                  TAG_TRIPNAME},
                          new int[] { R.id.pid, R.id.name });
                  // updating listview
                  ((ListView) lv.findViewById(R.id.list)).setAdapter(adapter);
              }
          });
       }
   }
  </code></pre>
  
  <p>}</p>