title: How to pass values from fragments in one activity to another activity?
body: <p><br> I am making a little quiz in an app that has two activities. one activity has fragments of the questions, and each question is answered with an <code>editText</code>. The fragments are contained inside a <code>FrameLayout</code> inside that activity, and when I reach the last question I have a &quot;Finish&quot; button.</p>
  <p>What I want is that when someone finishes the quiz and clicks on the &quot;Finish&quot; button that is in the last fragment, it will move him to the second activity and check the results of the questions.</p>
  <p>In other words, how can I get the values that were typed inside the <code>editText</code>s in the previous activity from each fragment in order to check the answers in the second activity and show the results?</p>
  <p>The quiz activity (main activity)</p>
  <pre><code>package com.example.ishihara_test
  
  import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import android.view.View
  import android.widget.Button
  
  class MainActivity : AppCompatActivity() {
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.main_activity)
  
          val plate1 = Frag_Plate1()
          val plate2 = Frag_Plate2()
          val plate3 = Frag_Plate3()
  
          val btnPrev = findViewById&lt;Button&gt;(R.id.btnPrev)
          val btnNext = findViewById&lt;Button&gt;(R.id.btnNext)
  
  
          supportFragmentManager.beginTransaction().apply {
              replace(R.id.flfragment, plate1)
              commit()
          }
  
          btnPrev.setOnClickListener {
              supportFragmentManager.beginTransaction().apply {
                  if (plate2.isVisible) {
                      replace(R.id.flfragment, plate1)
                  }
                  if (plate3.isVisible) {
                      replace(R.id.flfragment, plate2)
                      btnNext.text = &quot;Next&quot;
                  }
                  commit()
              }
          }
  
          btnNext.setOnClickListener {
              supportFragmentManager.beginTransaction().apply {
                  if (plate1.isVisible) {
                      replace(R.id.flfragment, plate2)
                  }
                  if (plate2.isVisible) {
                      replace(R.id.flfragment, plate3)
                      btnNext.text = &quot;Finish&quot;
                  }
                  commit()
              }
          }
  
      }
  }
  </code></pre>
  <p>xml layout:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.MainActivity&quot;&gt;
  
      &lt;Button
          android:id=&quot;@+id/btnPrev&quot;
          android:layout_width=&quot;129dp&quot;
          android:layout_height=&quot;45dp&quot;
          android:text=&quot;Previous&quot; /&gt;
  
      &lt;Button
          android:id=&quot;@+id/btnNext&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;Next&quot; /&gt;
  
      &lt;FrameLayout
          android:id=&quot;@+id/flfragment&quot;
          android:layout_width=&quot;match_parent&quot;
          android:layout_height=&quot;match_parent&quot; /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>all the fragments have this code:</p>
  <pre><code>package com.example.ishihara_test
  
  import android.os.Bundle
  import androidx.fragment.app.Fragment
  import android.view.LayoutInflater
  import android.view.View
  import android.view.ViewGroup
  import android.widget.EditText
  
  /**
   * A simple [Fragment] subclass.
   * Use the [Frag_Plate2.newInstance] factory method to
   * create an instance of this fragment.
   */
  class Frag_Plate2 : Fragment(R.layout.frag_plate2) {
      
  }
  </code></pre>
  <p>xml for fragments:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      tools:context=&quot;.Frag_Plate2&quot;&gt;
  
      &lt;!-- TODO: Update blank fragment layout --&gt;
  
      &lt;TextView
          android:id=&quot;@+id/plate2&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:text=&quot;What is the number on the plate?&quot;
          android:textSize=&quot;20sp&quot; /&gt;
  
      &lt;ImageView
          android:id=&quot;@+id/imageView&quot;
          android:layout_width=&quot;395dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:src=&quot;@drawable/plate2&quot; /&gt;
  
      &lt;EditText
          android:id=&quot;@+id/editTextTextAnswer&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:maxLines=&quot;1&quot;
          android:inputType=&quot;number&quot;
          android:hint=&quot;Write number here...&quot; /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  <p>I prefer in kotlin, but java code also works in order to understand.
  <br> <em>Note: I didn't put code snippets cause it's clearer what I said above, but if it helps I'll put.</em></p>
  <p>Any help is greatly appreciated, thanks a lot :)</p>