title: Using Fragment with tabs .. The specified child already has a parent
body: <p>I'm building an app like WhatsApp using 3 tabs and every tab have a <code>Fragment</code>. In my first <code>Fragment</code> I have a <code>RecyclerView</code>.</p>
  
  <p>Win a set the SectionsPagerAdapter to start width the RecyclerView Fragment 
  like this:</p>
  
  <pre><code>inner class SectionsPagerAdapter(fm: FragmentManager) : FragmentPagerAdapter(fm) {
  
          override fun getItem(position: Int): Fragment {
              // getItem is called to instantiate the fragment for the given page.
              // Return a PlaceholderFragment (defined as a static inner class below).
              //return PlaceholderFragment.newInstance(position + 1)
              when(position){
                  0 -&gt;  return Fragmenttask.newInstance()
                  1 -&gt;  return FragmentChat.newInstance()
                  2 -&gt;  return FragmentMaps.newInstance()
                  else  -&gt; {
                      return null!!
                  }
              }
          }
  
          override fun getCount(): Int {
              // Show 3 total pages.
              return 3
          }
      }
  </code></pre>
  
  <p>It give me this error:</p>
  
  <blockquote>
    <p>E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.deraah.mohamed.deraahpro, PID: 21993
                                                                              java.lang.IllegalStateException: The specified child already has a
    parent. You must call removeView() on the child's parent first.
                                                                                  at android.view.ViewGroup.addViewInner(ViewGroup.java:4937)
                                                                                  at android.view.ViewGroup.addView(ViewGroup.java:4768)
                                                                                  at android.support.v4.view.ViewPager.addView(ViewPager.java:1477)
                                                                                  at android.view.ViewGroup.addView(ViewGroup.java:4708)
                                                                                  at android.view.ViewGroup.addView(ViewGroup.java:4681)
                                                                                  at
    android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1425)
                                                                                  at
    android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
                                                                                  at
    android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:794)
                                                                                  at
    android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
                                                                                  at
    android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
                                                                                  at
    android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
                                                                                  at
    android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2199)
                                                                                  at
    android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:651)
                                                                                  at
    android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)
                                                                                  at android.support.v4.view.ViewPager.populate(ViewPager.java:1236)
                                                                                  at
    android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:662)
                                                                                  at
    android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:624)
                                                                                  at
    android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:605)
                                                                                  at
    android.support.design.widget.TabLayout$ViewPagerOnTabSelectedListener.onTabSelected(TabLayout.java:2170)
                                                                                  at
    android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1165)
                                                                                  at
    android.support.design.widget.TabLayout.selectTab(TabLayout.java:1158)
                                                                                  at
    android.support.design.widget.TabLayout.selectTab(TabLayout.java:1128)
                                                                                  at
    android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1427)
                                                                                  at
    android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1537)
                                                                                  at android.view.View$PerformClick.run(View.java:24770)
                                                                                  at android.os.Handler.handleCallback(Handler.java:790)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                  at android.os.Looper.loop(Looper.java:164)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at
    com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)</p>
  </blockquote>
  
  <p><strong>UPDATE</strong> </p>
  
  <p>My fragments:</p>
  
  <p>FragmentChat:</p>
  
  <pre><code>/**
   * A simple [Fragment] subclass.
   * Activities that contain this fragment must implement the
   * [FragmentChat.OnFragmentInteractionListener] interface
   * to handle interaction events.
   * Use the [FragmentChat.newInstance] factory method to
   * create an instance of this fragment.
   */
  class FragmentChat : Fragment() {
  
      private var mListener: OnFragmentInteractionListener? = null
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
      }
  
      override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                                savedInstanceState: Bundle?): View? {
          // Inflate the layout for this fragment
          return inflater!!.inflate(R.layout.fragment_fragment_chat, container, false)
      }
  
      // TODO: Rename method, update argument and hook method into UI event
      fun onButtonPressed(uri: Uri) {
          if (mListener != null) {
              mListener!!.onFragmentInteraction(uri)
          }
      }
  
      override fun onAttach(context: Context?) {
          super.onAttach(context)
      }
  
      override fun onDetach() {
          super.onDetach()
          mListener = 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.
       *
       *
       * See the Android Training lesson [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html) for more information.
       */
      interface OnFragmentInteractionListener {
          // TODO: Update argument type and name
          fun onFragmentInteraction(uri: Uri)
      }
  
      companion object {
  
          // TODO: Rename and change types and number of parameters
          fun newInstance(): FragmentChat {
              val fragment = FragmentChat()
              val args = Bundle()
              fragment.arguments = args
              return fragment
          }
      }
  }// Required empty public constructor
  </code></pre>
  
  <p>FragmentMaps:</p>
  
  <pre><code>/**
   * A simple [Fragment] subclass.
   * Activities that contain this fragment must implement the
   * [FragmentMaps.OnFragmentInteractionListener] interface
   * to handle interaction events.
   * Use the [FragmentMaps.newInstance] factory method to
   * create an instance of this fragment.
   */
  class FragmentMaps : Fragment() {
  
      private var mListener: OnFragmentInteractionListener? = null
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
      }
  
      override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                                savedInstanceState: Bundle?): View? {
          // Inflate the layout for this fragment
          return inflater!!.inflate(R.layout.fragment_fragment_maps, container, false)
      }
  
      // TODO: Rename method, update argument and hook method into UI event
      fun onButtonPressed(uri: Uri) {
          if (mListener != null) {
              mListener!!.onFragmentInteraction(uri)
          }
      }
  
      override fun onDetach() {
          super.onDetach()
          mListener = null
      }
  
  
      interface OnFragmentInteractionListener {
          // TODO: Update argument type and name
          fun onFragmentInteraction(uri: Uri)
      }
  
      companion object {
          // TODO: Rename and change types and number of parameters
          fun newInstance(): FragmentMaps {
              val fragment = FragmentMaps()
              val args = Bundle()
              fragment.arguments = args
              return fragment
          }
      }
  }// Required empty public constructor
  </code></pre>
  
  <p>Fragmenttask :</p>
  
  <pre><code>class Fragmenttask : Fragment() {
  
      private var mListener: OnFragmentInteractionListener? = null
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
      }
  
      override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?,
                                savedInstanceState: Bundle?): View? {
          val CustumViewtask = inflater!!.inflate(R.layout.fragment_fragmenttask, container, false)
          val taskMainRV = CustumViewtask.findViewById(R.id.recyclerView_main) as RecyclerView
          //taskMainRV.setBackgroundColor(Color.BLUE)
  
          taskMainRV.layoutManager = LinearLayoutManager(context)
          //recyclerView_main.adapter = MainAdapter()
  
          fetchJson(taskMainRV)
  
          return taskMainRV
      }
  
      fun fetchJson(RSV: RecyclerView) {
  
          //SharedPreferences
          val MY_APP_INFO: String = "UserInfo"
          val prefs = activity.getSharedPreferences(MY_APP_INFO, AppCompatActivity.MODE_PRIVATE)
          val LoggedUserId = prefs.getString("UserId", null)
          println("your code is : $LoggedUserId")
  
          println("Attempting to Fetch JSON")
  
          val url = "http://Restful.com/get_tasks.php"
  
          val client = OkHttpClient()
  
          val formBody = FormBody.Builder().add("UserId", LoggedUserId).build()
  
          val request = Request.Builder().url(url)
                  .post(formBody)
                  .build()
  
          client.newCall(request).enqueue(object: Callback {
              override fun onResponse(call: Call?, response: Response?) {
                  val body = response?.body()?.string()
                  println("mohamed : $body")
  
                  val gson = GsonBuilder().create()
  
                  val tasksfeed = gson.fromJson(body, M_tasksFeed::class.java)
  
                  activity.runOnUiThread {
                      RSV.adapter = MainAdaptertasks(tasksfeed)
                  }
  
              }
  
              override fun onFailure(call: Call?, e: IOException?) {
                  println("Failed to execute request")
              }
          })
      }
  
      // TODO: Rename method, update argument and hook method into UI event
      fun onButtonPressed(uri: Uri) {
          if (mListener != null) {
              mListener!!.onFragmentInteraction(uri)
          }
      }
  
      override fun onDetach() {
          super.onDetach()
          mListener = 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.
       *
       *
       * See the Android Training lesson [Communicating with Other Fragments](http://developer.android.com/training/basics/fragments/communicating.html) for more information.
       */
      interface OnFragmentInteractionListener {
          // TODO: Update argument type and name
          fun onFragmentInteraction(uri: Uri)
      }
  
      companion object {
          fun newInstance(): Fragmenttask {
              val fragment = Fragmenttask()
              val args = Bundle()
              fragment.arguments = args
              return fragment
          }
      }
  }// Required empty public constructor
  </code></pre>