title: Remove Activity Views after Fragment Loads
body: <p>I have an activity that hosts a fragment. The activity essentially has no content except a spinner that indicates the fragment is loading. The fragment is dependent upon a stable internet connection, therefore the length of time required for the spinner to be visible is dynamic in nature.</p>
  
  <p>I want to remove the spinner on the activity after the fragment successfully loads. I tried using the isAdded() method, however that approach did not work. Any help is appreciated:</p>
  
  <p>Fragment:</p>
  
  <pre><code>public class LatestFragment extends Fragment {
  // TODO: Rename parameter arguments, choose names that match
  // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  private static final String ARG_PARAM1 = "param1";
  private static final String ARG_PARAM2 = "param2";
  
  private RecyclerView mRecyclerViewForLatestPolls;
  private RecyclerView.Adapter mLatestAdapter;
  
  private ArrayList&lt;LatestPoll&gt; mLatestPollsArray;
  
  private DateFormat mDateFormat;
  private Date mDate;
  private String mCurrentDateString;
  
  private Firebase mBaseRef;
  private Firebase mPollRef;
  private Firebase mUpdateRef;
  
  private FragmentListener mFragmentListener;
  
  // TODO: Rename and change types of parameters
  private String mParam1;
  private String mParam2;
  
  
  private OnFragmentInteractionListener mListener;
  
  public LatestFragment() {
      // Required empty public constructor
  }
  
  /**
   * Use this factory method to create a new instance of
   * this fragment using the provided parameters.
   *
   * @param param1 Parameter 1.
   * @param param2 Parameter 2.
   * @return A new instance of fragment LatestFragment.
   */
  // TODO: Rename and change types and number of parameters
  public static LatestFragment newInstance(String param1, String param2) {
      LatestFragment fragment = new LatestFragment();
      Bundle args = new Bundle();
      args.putString(ARG_PARAM1, param1);
      args.putString(ARG_PARAM2, param2);
      fragment.setArguments(args);
      return fragment;
  }
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      mDateFormat = new SimpleDateFormat("MM-dd-yyyy");
      mDate = new Date();
      mCurrentDateString = mDateFormat.format(mDate);
      mBaseRef = FirebaseUtil.FIREBASE;
      mPollRef = mBaseRef.child("Polls");
      mUpdateRef = mPollRef.child(mCurrentDateString);
  
  
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                           Bundle savedInstanceState) {
  
      final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_latest, container, false);
      getActivity().setTitle(R.string.latest_title);
  
      mRecyclerViewForLatestPolls = (RecyclerView) rootView.findViewById(R.id.latest_RecyclerView);
      mLatestPollsArray = new ArrayList&lt;&gt;();
      mLatestAdapter = new MyAdapter(mLatestPollsArray);
      LinearLayoutManager llm = new LinearLayoutManager(getActivity().getApplicationContext());
      llm.setOrientation(LinearLayoutManager.VERTICAL);
      mRecyclerViewForLatestPolls.setLayoutManager(llm);
      mRecyclerViewForLatestPolls.setItemAnimator(new SlideInLeftAnimator());
      mRecyclerViewForLatestPolls.setAdapter(new AlphaInAnimationAdapter(mLatestAdapter));
  
      mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              int numberOfPollsForDay = (int) dataSnapshot.getChildrenCount();
              for (int i = 0; i &lt; numberOfPollsForDay; i++) {
                  String latestPollQuestion = (String) dataSnapshot.child(String.valueOf(i + 1)).child("Poll_Question").getValue();
                  String pollImageURL = (String) dataSnapshot.child(String.valueOf(i + 1)).child("Image").getValue();
                  mLatestPollsArray.add(0, new LatestPoll(latestPollQuestion, pollImageURL));
                  mLatestAdapter.notifyDataSetChanged();
              }
          }
  
          @Override
          public void onCancelled(FirebaseError firebaseError) {
  
          }
      });
  
  
      // Inflate the layout for this fragment
      return rootView;
  }
  
  
  // TODO: Rename method, update argument and hook method into UI event
  public void onButtonPressed(Uri uri) {
      if (mListener != null) {
          mListener.onFragmentInteraction(uri);
      }
  }
  
  @Override
  public void onAttach(Context context) {
      super.onAttach(context);
      if (context instanceof OnFragmentInteractionListener) {
          mListener = (OnFragmentInteractionListener) context;
      } else {
          throw new RuntimeException(context.toString()
                  + " must implement OnFragmentInteractionListener");
      }
  
      // Force the parent activity to implement listener.
      if (context instanceof FragmentListener) {
          mFragmentListener = (FragmentListener) context;
      } else {
          throw new RuntimeException(context.toString()
                  + " must implement OnFragmentInteractionListener");
      }
  }
  
  @Override
  public void onDetach() {
      super.onDetach();
      mListener = null;
      mFragmentListener = null;
  }
  
  /**
   * This interface must be implemented by activities that contain this
   * fragment to allow an interaction in this fragment to be communicated
   * to the activity and potentially other fragments contained in that
   * activity.
   * &lt;p/&gt;
   * See the Android Training lesson &lt;a href=
   * "http://developer.android.com/training/basics/fragments/communicating.html"
   * &gt;Communicating with Other Fragments&lt;/a&gt; for more information.
   */
  public interface OnFragmentInteractionListener {
      // TODO: Update argument type and name
      void onFragmentInteraction(Uri uri);
  }
  
  public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
  
  
      private ArrayList&lt;LatestPoll&gt; mDataSet;
      int lastPosition = -1;
  
  
      // Provide a reference to the views for each data item
      // Complex data items may need more than one view per item, and
      // you provide access to all the views for a data item in a view holder
      public class ViewHolder extends RecyclerView.ViewHolder {
          // each data item is just a string in this case
          protected TextView pollQuestion;
          protected ImageView pollImage;
  
  
          public ViewHolder(View v) {
              super(v);
              pollQuestion = (TextView) v.findViewById(R.id.latest_item_question);
              pollImage = (ImageView) v.findViewById(R.id.pollThumbNailImage);
  
  
          }
      }
  
      // Provide a suitable constructor (depends on the kind of dataset)
      public MyAdapter(ArrayList&lt;LatestPoll&gt; myDataset) {
          mDataSet = myDataset;
      }
  
      // Create new views (invoked by the layout manager)
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent,
                                           int viewType) {
          // create a new view
          View v = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.latest_item, parent, false);
          // set the view's size, margins, paddings and layout parameters
          return new ViewHolder(v);
      }
  
      // Replace the contents of a view (invoked by the layout manager)
  
      //The OutOfBoundsException is pointing here
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
          Log.v("ON_BIND", "ON_BINDVIEWHOLDER CALLED");
          LatestPoll latestPoll = mDataSet.get(position);
          holder.pollQuestion.setText(latestPoll.getQuestion());
          Picasso.with(getActivity())
                  .load(latestPoll.getPollImage())
                  .fit()
                  .placeholder(R.drawable.loading_spinnter_white)
                  .into(holder.pollImage);
      }
  
  
  
  
      // Return the size of your dataset (invoked by the layout manager)
      @Override
      public int getItemCount() {
          return mDataSet.size();
      }
  }
  
  private void onLoad() {
      if (mFragmentListener != null) {
          mFragmentListener.onFragmentLoaded();
      }
  }
  
  public interface FragmentListener {
      void onFragmentLoaded();
  }
  
  
  }
  </code></pre>
  
  <p>Activity XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &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"
  android:orientation="vertical"&gt;
  
  &lt;LinearLayout
      android:id="@+id/content_frame"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/black"
      android:orientation="vertical"&gt;
  
      &lt;android.support.v7.widget.Toolbar
          android:id="@+id/action_tool_bar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:background="@color/black"
          android:titleTextColor="@color/white"&gt;
  
      &lt;/android.support.v7.widget.Toolbar&gt;
  
      &lt;FrameLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"&gt;
  
          &lt;RelativeLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
  
              &lt;ProgressBar
                  android:id="@+id/pbHeaderProgress"
                  android:layout_width="150dp"
                  android:layout_height="150dp"
                  android:layout_centerInParent="true"
                  android:progressDrawable="@drawable/loading_spinnter_white"&gt;
              &lt;/ProgressBar&gt;
  
              &lt;TextView
                  android:id="@+id/progress_text"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_below="@+id/pbHeaderProgress"
                  android:layout_centerHorizontal="true"
                  android:layout_gravity="center_horizontal"
                  android:gravity="center"
                  android:text="@string/loading_poll_data"
                  android:textColor="@color/white"
                  android:textSize="24sp" /&gt;
          &lt;/RelativeLayout&gt;
  
          &lt;FrameLayout
              android:id="@+id/latest_fragment_container"
              android:layout_width="match_parent"
              android:layout_height="match_parent"&gt;
          &lt;/FrameLayout&gt;
      &lt;/FrameLayout&gt;
  &lt;/LinearLayout&gt;
  
  &lt;!-- The navigation drawer --&gt;
  &lt;ListView
      android:id="@+id/left_drawer"
      android:layout_width="240dp"
      android:layout_height="match_parent"
      android:layout_gravity="start"
      android:background="#111"
      android:choiceMode="singleChoice"
      android:divider="@android:color/transparent"
      android:dividerHeight="0dp" /&gt;
  </code></pre>
  
  <p></p>
  
  <p>Image:
  <a href="https://i.stack.imgur.com/YCBbM.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YCBbM.png" alt="enter image description here"></a></p>