title: Fragment Is shown part of the time
body: <p>I have A navigation drawer with several fragments in the menu, in the first fragment when an item is pressed in a gridView i want to start a different fragment, i get no error , but sometimes the fragment is shown and sometimes it's not shown,</p>
  
  <p>thc calling fragment is HomeFragment:</p>
  
  <p>the opend fragment is InstrumentsFragment , </p>
  
  <p>here is the container layout:  drawr layout:  </p>
  
  <pre><code>&lt;android.support.v4.widget.DrawerLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/drawer_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent"&gt;
  
      &lt;!-- Framelayout to display Fragments --&gt;
      &lt;FrameLayout
          android:id="@+id/frame_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent" /&gt;
  
      &lt;!-- Listview to display slider menu --&gt;
      &lt;ListView
          android:id="@+id/list_slidermenu"
          android:layout_width="240dp"
          android:layout_height="match_parent"
          android:layout_gravity="start"
          android:choiceMode="singleChoice"
          android:divider="@color/list_divider"
          android:dividerHeight="1dp"       
          android:listSelector="@drawable/list_selector"
          android:background="@color/list_background"/&gt;
  &lt;/android.support.v4.widget.DrawerLayout&gt;
  </code></pre>
  
  <p>all the other fragments beside InstrumentsFragment are called form an activity: with this method:  </p>
  
  <pre><code>private void displayView(int position) {
      // update the main content by replacing fragments
      Fragment fragment = null;
      switch (position) {
      case 0:
          fragment = new FirstFragment();
          break;
      case 1:
          fragment = new SecondFragment();
          break;
      case 2:
          fragment = new SearchDbFragment();
          break;
      case 3:
          fragment = new ThirdFragment();
          break;
      default:
          break;
      }
  
      if (fragment != null) {
          FragmentManager fragmentManager = getFragmentManager();
          fragmentManager.beginTransaction()
          .replace(R.id.frame_container, fragment).commit();
          // update selected item and title, then close the drawer
          mDrawerList.setItemChecked(position, true);
          mDrawerList.setSelection(position);
          setTitle(navMenuTitles[position]);
          mDrawerLayout.closeDrawer(mDrawerList);
      } else {
          // error in creating fragment
          Log.e("MainActivity", "Error in creating fragment");
      }
  }
  </code></pre>
  
  <p>so from HomeFragment to InstrumentsFragment i'm calling in this way: </p>
  
  <pre><code>final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
                      ft.replace(R.id.frame_container, new InstrumentsFragment(), "InstrumentsFragment"); 
                      ft.addToBackStack(null);
                      ft.commit();
  </code></pre>
  
  <p>and i'm getting no error! , just sometimes it works and sometimes it dosn't!
  what can be the problem?</p>
  
  <p>EDIT: </p>
  
  <p>full code:  </p>
  
  <p>the class that contains the navigation drawer:</p>
  
  <pre><code>public class MainOptionsActivity extends Activity 
  {
      private DrawerLayout mDrawerLayout;
      private ListView mDrawerList;
      private ActionBarDrawerToggle mDrawerToggle;
      ParseUser user = ParseUser.getCurrentUser();
      // nav drawer title
      private CharSequence mDrawerTitle;
      ArrayList&lt;String&gt; resultCompanys = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; resultModels = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; resultPrices = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; resultStores = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; resultUrls = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; followedObjIds = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; urlsForUpdate = new ArrayList&lt;String&gt;();
      boolean changeInFollowedItems = false;
      String currentUrl = "";
      String[] urlsArray = {"http://www.music-","http://www.kolhamusica" , "http://www.diez.co"};
      // used to store app title
      private CharSequence mTitle;
      protected OnBackPressedListener onBackPressedListener;
      // slide menu items
      private String[] navMenuTitles;
      private TypedArray navMenuIcons;
      private ArrayList&lt;NavDrawerItem&gt; navDrawerItems;
      private NavDrawerListAdapter adapter;
      int locationOfCompany = 0 , fragmentIdFromIntent = -1;
      String tmpCompany = "";
      CustomAdapter followsAdapter;
  
      boolean doubleBackToExitPressedOnce = false;
  
      String urlToFetchFrom = "" ;
  
      ArrayList&lt;String&gt; gPricesAsist = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; afterUpdatePrices = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; changedPricesObjIds = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; ObjIdsForNotifications = new ArrayList&lt;String&gt;();
  
      SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
      ArrayList&lt;ParseObject&gt; alPobjs = new ArrayList&lt;ParseObject&gt;();
      ParseObject pObj ;
      String urlForUpdatePrice = "" , objIdToUpdate = "";
      String finalPriceForUpdate = "";
      String currentOldPrice="";
      int location;
      Date currentDate;
  
      String[] months = {"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
      String[] monthsInNumsFormat = {"0","1","2","3","4","5","6","7","8","9","10","11"};
      boolean[] pricesWereChanged;
  
      String[] urlsForJsoup;
      boolean userHasFollowedItems = false;
      Document doc;
      ParseQuery&lt;ParseObject&gt; pq;
      Context context = this;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.drawr_layout);
          /////////////////////////////////////////////////////////////////////////
  
          mTitle = mDrawerTitle = getTitle();
          //////////////////////////////////////////////////////////////////////
          // load slide menu items
          navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
  
          // nav drawer icons from resources
          navMenuIcons = getResources()
                  .obtainTypedArray(R.array.nav_drawer_icons);
  
          mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
          mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
  
          navDrawerItems = new ArrayList&lt;NavDrawerItem&gt;();
  
          // adding nav drawer items to array
          // Home
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
          // Find People
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
          // Photos
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[2], navMenuIcons.getResourceId(2, -1)));
          // Communities, Will add a counter here
          navDrawerItems.add(new NavDrawerItem(navMenuTitles[3], navMenuIcons.getResourceId(3, -1)));
  
  
          // Recycle the typed array
          navMenuIcons.recycle();
  
          mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
  
          // setting the nav drawer list adapter
          adapter = new NavDrawerListAdapter(getApplicationContext(),
                  navDrawerItems);
          mDrawerList.setAdapter(adapter);
  
          // enabling action bar app icon and behaving it as toggle button
          getActionBar().setDisplayHomeAsUpEnabled(true);
          getActionBar().setHomeButtonEnabled(true);
  
          mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
                  R.drawable.ic_drawer, //nav menu toggle icon
                  R.string.app_name, // nav drawer open - description for accessibility
                  R.string.app_name // nav drawer close - description for accessibility
                  ) {
              public void onDrawerClosed(View view) {
                  getActionBar().setTitle(mTitle);
                  // calling onPrepareOptionsMenu() to show action bar icons
                  invalidateOptionsMenu();
              }
  
              public void onDrawerOpened(View drawerView) {
                  getActionBar().setTitle(mDrawerTitle);
                  // calling onPrepareOptionsMenu() to hide action bar icons
                  invalidateOptionsMenu();
              }
          };
          mDrawerLayout.setDrawerListener(mDrawerToggle);
  
          if (savedInstanceState == null) {
              // on first time display view for first nav item
              displayView(0);
          }
          fragmentIdFromIntent = getIntent().getIntExtra("fragmentIdFromIntent", -1);
          if(fragmentIdFromIntent != -1)
          {
              displayView(fragmentIdFromIntent-1);
          }
      }
  
      /**
       * Slide menu item click listener
       * */
      public class SlideMenuClickListener implements
      ListView.OnItemClickListener {
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
                  long id) {
              // display view for selected nav drawer item
              displayView(position);
          }
  
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // toggle nav drawer on selecting action bar app icon/title
          if (mDrawerToggle.onOptionsItemSelected(item)) {
              return true;
          }
          // Handle action bar actions click
          switch (item.getItemId()) {
          default:
              return super.onOptionsItemSelected(item);
          }
      }
  
      /* *
       * Called when invalidateOptionsMenu() is triggered
       */
      @Override
      public boolean onPrepareOptionsMenu(Menu menu) {
          // if nav drawer is opened, hide the action items
          boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
          return super.onPrepareOptionsMenu(menu);
      }
  
      /**
       * Diplaying fragment view for selected nav drawer list item
       * */
      private void displayView(int position) {
          // update the main content by replacing fragments
          Fragment fragment = null;
          switch (position) {
          case 0:
              fragment = new HomeFragment();
              break;
          case 1:
              fragment = new SecondFragment();
              break;
          case 2:
              fragment = new ThirdFragment();
              break;
          case 3:
              fragment = new ForthFragment();
              break;
  
          default:
              break;
          }
  
          if (fragment != null) {
              FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction()
              .replace(R.id.frame_container, fragment).commit();
              // update selected item and title, then close the drawer
              mDrawerList.setItemChecked(position, true);
              mDrawerList.setSelection(position);
              setTitle(navMenuTitles[position]);
              mDrawerLayout.closeDrawer(mDrawerList);
          } else {
              // error in creating fragment
              Log.e("MainActivity", "Error in creating fragment");
          }
      }
  
      @Override
      public void setTitle(CharSequence title) {
          mTitle = title;
          getActionBar().setTitle(mTitle);
      }
  
      /**
       * When using the ActionBarDrawerToggle, you must call it during
       * onPostCreate() and onConfigurationChanged()...
       */
  
      @Override
      protected void onPostCreate(Bundle savedInstanceState) {
          super.onPostCreate(savedInstanceState);
          // Sync the toggle state after onRestoreInstanceState has occurred.
          mDrawerToggle.syncState();
      }
  
      @Override
      public void onConfigurationChanged(Configuration newConfig) {
          super.onConfigurationChanged(newConfig);
          // Pass any configuration change to the drawer toggls
          mDrawerToggle.onConfigurationChanged(newConfig);
      }
  }
  </code></pre>
  
  <p>full home fragment: </p>
  
  <pre><code>public class HomeFragment extends Fragment 
  {
      ArrayList&lt;String&gt; CompanyResults = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; ModelsResults = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; PricesResults = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; urlForImgResults = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; storeResults = new ArrayList&lt;String&gt;();
      String[] types = {"Instruments"};
      int[] imgIds = {R.drawable.instruments_small_square_2};
      ArrayList&lt;HomeFragGridItem&gt; items = new ArrayList&lt;HomeFragGridItem&gt;();
      ArrayList&lt;GridItem&gt; gItems = new ArrayList&lt;GridItem&gt;();
      Context context;
      GridView homeGv;
      CustomAdapter gvAdapter;
      HomeFragmentGridViewAdapter hgvAdapter;
      ProgressBar pb;
      boolean doubleBackToExitPressedOnce = false;
      FragmentsComunicator fComunicator;
  
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) 
      {
  
          View rootView = inflater.inflate(R.layout.fragment_home, container, false);
          context = getActivity();
          Parse.initialize(context, "123456789", "123456789");
          homeGv = (GridView)rootView.findViewById(R.id.homeFragGridView);
          //pd = ProgressDialog.show(getActivity(), null, "loading...");
          pb=  (ProgressBar)rootView.findViewById(R.id.progressBar1);
          pb.setVisibility(View.VISIBLE);
          Log.i("WhicFragment?", "HOME FRAGMENT");
          Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.small_instruments);
          GridViewCustomAdapter gvAdapter = new GridViewCustomAdapter(context, R.layout.grid_view_item, gItems);
          gItems.add(new GridItem("Instruments", "289 items",bitmap));
          bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.small_smartphones);
          gItems.add(new GridItem("SmartPhones", "coming soon..",bitmap));
          bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.small_cameras);
          gItems.add(new GridItem("Cameras","coming soon..",bitmap));
          bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.small_fridges);
          gItems.add(new GridItem("Fridges", "coming soon..",bitmap));
          bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.small_laptops);
          gItems.add(new GridItem("Laptops", "coming soon..",bitmap));
  
  
          homeGv.setAdapter(gvAdapter);
          gvAdapter.notifyDataSetChanged();
          pb.setVisibility(View.GONE);
  
          homeGv.setOnItemClickListener(new OnItemClickListener() 
          {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) 
              {
                  if(position == 0)
                  {
                      //frame_container2  
                      //                  Intent intent = new Intent(getActivity(),InstrumentsActivity.class);
                      //                  startActivity(intent);
                      final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
                      ft.add(R.id.frame_container, new InstrumentsFragment(), "InstrumentsFragment"); 
                      //ft.addToBackStack(null);
                      ft.commit(); 
                  }
  
              }
  
          });
          return rootView;
  
      }
  
  }
  </code></pre>
  
  <p>full Instruments Fragment: </p>
  
  <pre><code>public class InstrumentsFragment  extends Fragment
  {
      CustomAdapter gvAdapter;
      HomeFragmentGridViewAdapter hgvAdapter;
      ProgressBar pb;
      GridView homeGv;
      ArrayList&lt;HomeFragGridItem&gt; items = new ArrayList&lt;HomeFragGridItem&gt;();
      ArrayList&lt;String&gt; CompanyResults = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; ModelsResults = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; PricesResults = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; urlForImgResults = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; storeResults = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; resultIds = new ArrayList&lt;String&gt;();
  
      Context context;
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) 
      {
          View rootView = inflater.inflate(R.layout.instrumnets_frag_layout, container,false);
          context = getActivity();
          Parse.initialize(context, "123456789", "123456789");
          homeGv = (GridView)rootView.findViewById(R.id.instrumentsFragGV);
          //pd = ProgressDialog.show(getActivity(), null, "loading...");
          pb=  (ProgressBar)rootView.findViewById(R.id.instrumentsPb);
          pb.setVisibility(View.VISIBLE);
          Log.i("WhicFragment?", "INSTRUMENTS FRAGMENT");
          container.removeAllViews();
          hgvAdapter = new HomeFragmentGridViewAdapter(getActivity(),R.layout.home_frag_gview_item_layout,items);
          homeGv.setAdapter(hgvAdapter);
  
          ParseQuery&lt;ParseObject&gt; pq = new ParseQuery&lt;ParseObject&gt;("ClassicGuitars");
          pq.whereExists("Type");
          pq.setLimit(1000);
          List&lt;ParseObject&gt; pObjs = null;
          try 
          {
              pObjs = pq.find();
          } 
          catch (ParseException e) 
          {
              e.printStackTrace();
          } catch (com.parse.ParseException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          if(pObjs != null)
          {
              Log.i("parse_results", "pares object list in grid view is NOT null");
  
              for (ParseObject parseObject : pObjs) 
              {
                  CompanyResults.add(parseObject.getString("InstrumentCompany"));
                  ModelsResults.add(parseObject.getString("InstrumentModel"));
                  PricesResults.add(parseObject.getString("LastUpdatedPrice"));
                  urlForImgResults.add(parseObject.getString("UrlForImg"));
                  storeResults.add(parseObject.getString("InstrumentUrl"));
                  resultIds.add(parseObject.getObjectId());
              }
          }
          BgWork b1 = new BgWork();
          b1.execute();
  
          hgvAdapter.notifyDataSetChanged();
          pb.setVisibility(View.GONE);
          homeGv.setOnItemClickListener(new OnItemClickListener() 
          {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view,
                      int position, long id) 
              {
                  Intent offersInent  = new Intent (getActivity(), Offers.class);
                  offersInent.putExtra("actvSelection",
                          CompanyResults.get(position).toString()+" "+ModelsResults.get(position).toString());
                  offersInent.putStringArrayListExtra("companysArray", CompanyResults);
                  offersInent.putStringArrayListExtra("modelsArray", ModelsResults);
                  offersInent.putStringArrayListExtra("pricesArray", PricesResults);
                  offersInent.putStringArrayListExtra("companysUrls", storeResults);
                  offersInent.putStringArrayListExtra("imgUrls", urlForImgResults);
                  offersInent.putStringArrayListExtra("ids", resultIds);
  
                  startActivity(offersInent);
  
              }
  
          });
          return rootView;
      }
      @Override
      public void onResume() 
      {
          super.onResume();
  
          getView().setFocusableInTouchMode(true);
          getView().requestFocus();
          getView().setOnKeyListener(new View.OnKeyListener() 
          {
              @Override
              public boolean onKey(View v, int keyCode, KeyEvent event) 
              {
  
                  if (event.getAction() == KeyEvent.ACTION_UP &amp;&amp; keyCode == KeyEvent.KEYCODE_BACK)
                  {
                      Intent intent = new Intent(getActivity(),MainOptionsActivity.class);
                      startActivity(intent);
                      getActivity().finish();
                      return true;
                  }
                  return false;
              }
          });
      }
      private class BgWork extends AsyncTask&lt;Void, Void, Void&gt;
      {
          @Override
          protected Void doInBackground(Void... params) 
          {
              for (int  i = 0 ; i &lt; CompanyResults.size() ; i ++)
              {
                  HomeFragGridItem hgItem = new HomeFragGridItem(urlForImgResults.get(i).toString(),
                          CompanyResults.get(i).toString(),
                          ModelsResults.get(i).toString(),
                          PricesResults.get(i).toString());
                  items.add(hgItem);
              }
              return null;
          }
      }
  }
  </code></pre>
  
  <p>adapter:  </p>
  
  <pre><code>public class HomeFragmentGridViewAdapter extends  ArrayAdapter&lt;HomeFragGridItem&gt;
  {
      ArrayList&lt;String&gt; companys = new ArrayList&lt;String&gt;();   
      ArrayList&lt;String&gt; models = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; prices = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; stores = new ArrayList&lt;String&gt;();
      ArrayList&lt;String&gt; urls = new ArrayList&lt;String&gt;();
      private int layoutResourceId;
      private ArrayList&lt;HomeFragGridItem&gt; mGridData = new ArrayList&lt;HomeFragGridItem&gt;();
  
      Context context;
      String url =""
      private static LayoutInflater inflater=null;
  
      public HomeFragmentGridViewAdapter(Context context,int layoutResourceId, ArrayList&lt;HomeFragGridItem&gt; mGridData) 
      {
  
          super(context, layoutResourceId, mGridData);
          this.context = context;
          this.layoutResourceId = layoutResourceId;
          this.mGridData = mGridData; 
      }
  
  
  
  
      public void setGridData(ArrayList&lt;HomeFragGridItem&gt; mGridData)
      {
          this.mGridData = mGridData;
          notifyDataSetChanged();
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) 
      {
          View row = convertView;
          ViewHolder holder;
          if(row == null)
          {
              LayoutInflater inflater = ((Activity) context).getLayoutInflater();
              row = inflater.inflate(layoutResourceId, parent, false);
              holder = new ViewHolder();
  
              holder.guitarCompanyTv=(TextView) row.findViewById(R.id.gridViewItemCompanyTv);
              holder.guitarModelTv=(TextView) row.findViewById(R.id.gridViewItemModelTv);
              holder.guitarImg=(ImageView) row.findViewById(R.id.gridViewItemIv); 
              holder.guitarPriceTv=(TextView) row.findViewById(R.id.gridViewItemPriceTv);
              row.setTag(holder);
  
          }
          else
          {
              holder = (ViewHolder) row.getTag();
          }
          HomeFragGridItem item = mGridData.get(position);
          holder.guitarCompanyTv.setText(item.getCompany());
          holder.guitarModelTv.setText(item.getMoedl());
          holder.guitarPriceTv.setText(item.getPrice());
          //Log.i("parse_results", "*** 2 **"+(urls.get(position)));
          Picasso.with(context).load(item.getImage()).resize(140, 190).centerCrop().into(holder.guitarImg);       
  
          return row;
      }
      public class ViewHolder
      {
          TextView guitarCompanyTv;
          TextView guitarModelTv;
          TextView guitarPriceTv;
          ImageView guitarImg;
      }
  
  }
  </code></pre>