title: Passing arguments between Fragments that are inside a ViewPager
body: <p>I'm working with different Fragments that are hosted by a ViewPager.</p>
  
  <p>The main purpose of doing this is because I need to convert a paper form into a digital form, and for user-friendly purposes, I decided to let the user swipe between all the fragments and complete each of them until the end of the form.</p>
  
  <p>My goal is to construct an object at the end of the latest swiped fragment, so, at that time I will have all the data coming from the below fragments.</p>
  
  <p>I have researched and found that I can use <code>setArguments()</code> to pass a bundle of data from each fragment to the next until the end, and this is a great idea since if the user wants to come back, they can change some arguments and keep the process of swiping until the end, and, at the last fragment, I will have all the arguments from all the fragments to create a unique object containing all the data that represents the entire form.</p>
  
  <p>This is what I have tried</p>
  
  <p>First I created a simple <code>ViewPager</code> adapter to switch between different fragments when I swipe</p>
  
  <h2>MyPagerAdapter</h2>
  
  <pre><code>class MyPagerAdapter(fm:FragmentManager): FragmentStatePagerAdapter(fm) {
  
      override fun getItem(position: Int): Fragment {
          return when(position){
              0 -&gt; FragmentA.newInstance()
              1 -&gt; FragmentB.newInstance()
              2 -&gt; FragmentC.newInstance()
              else -&gt; {
                  FragmentA()
              }
          }
      }
  
      override fun getCount(): Int {
          return 3
      }
  }
  </code></pre>
  
  <p>Then I just use this adapter with my viewpager at my MainActivity</p>
  
  <h2>MainActivity</h2>
  
  <pre><code>class MainActivity : FragmentActivity(), MainContract.MainView {
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          setupViewPager()
      }
  
      override fun setupViewPager() {
          viewPager.adapter = MyPagerAdapter(supportFragmentManager)
          viewPager.disableScroll(true)
          selectIndex(0)
      }
  
      override fun selectIndex(newIndex: Int) {
          viewPager.currentItem = newIndex
      }
  
      override fun getFragment(position: Int): Fragment {
          return MyPagerAdapter(supportFragmentManager).getItem(position)
      }
  
  }
  </code></pre>
  
  <p>And this is where I need to pass arguments from <strong>FragmentA</strong> to <strong>FragmentB</strong></p>
  
  <h2>FragmentA</h2>
  
  <pre><code>class FragmentA : Fragment() {
  
      companion object {
          fun newInstance(): FragmentA = FragmentA()
      }
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
          return inflater.inflate(R.layout.fragment_A, container, false)
      }
  
      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
          super.onViewCreated(view, savedInstanceState)
  
          fab_continue.setOnClickListener {
  
              val name = etxt_name.text.toString().trim()
              val phone = etxt_phone.text.toString().trim()
  
              val bundle = Bundle()
              bundle.putString("name",name)
              bundle.putString("phone",phone)
  
              (activity as MainActivity).getFragment(1).arguments = bundle
              (activity as MainActivity).selectIndex(1)
          }
  
      }
  </code></pre>
  
  <p>Here I asume that </p>
  
  <pre><code>(activity as MainActivity).getFragment(1).arguments = bundle
  </code></pre>
  
  <p>gets and inflate the next fragment, this fragment is <strong>FragmentB</strong> and pass the arguments to it</p>
  
  <p>While </p>
  
  <pre><code>(activity as MainActivity).selectIndex(1)
  </code></pre>
  
  <p>Just swipe to the next page with the <strong>FragmentB</strong></p>
  
  <h2>The Problem</h2>
  
  <p>Now, the issue is that when the app launches, <strong>getArguments</strong> from <strong>FragmentB</strong> is immediately executed but I have not even swiped to it, or pass the arguments from <strong>FragmentA</strong>, and that generates null to <strong>getArguments</strong>. </p>
  
  <p>With the above code I did, I thought that after pressing my fab button from <strong>FragmentA</strong>, the <code>getFragment(1)</code> method will instantiate my <strong>FragmentB</strong>, but it seems is not doing what it should do.</p>
  
  <p>So at my <code>FragmentB</code></p>
  
  <pre><code>class FragmentB: Fragment() {
  
      companion object {
          fun newInstance(): FragmentB = FragmentB()
      }
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          Log.d("Argumentos:",""+arguments?.getString("name")) //This returns null
      }
  </code></pre>
  
  <p><code>arguments?.getString("name")</code> returns null</p>