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 editText
. The fragments are contained inside a FrameLayout
inside that activity, and when I reach the last question I have a "Finish" button.
What I want is that when someone finishes the quiz and clicks on the "Finish" button that is in the last fragment, it will move him to the second activity and check the results of the questions.
In other words, how can I get the values that were typed inside the editText
s in the previous activity from each fragment in order to check the answers in the second activity and show the results?
The quiz activity (main activity)
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<Button>(R.id.btnPrev)
val btnNext = findViewById<Button>(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 = "Next"
}
commit()
}
}
btnNext.setOnClickListener {
supportFragmentManager.beginTransaction().apply {
if (plate1.isVisible) {
replace(R.id.flfragment, plate2)
}
if (plate2.isVisible) {
replace(R.id.flfragment, plate3)
btnNext.text = "Finish"
}
commit()
}
}
}
}
xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/btnPrev"
android:layout_width="129dp"
android:layout_height="45dp"
android:text="Previous" />
<Button
android:id="@+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next" />
<FrameLayout
android:id="@+id/flfragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
all the fragments have this 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) {
}
xml for fragments:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Frag_Plate2">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/plate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is the number on the plate?"
android:textSize="20sp" />
<ImageView
android:id="@+id/imageView"
android:layout_width="395dp"
android:layout_height="wrap_content"
android:src="@drawable/plate2" />
<EditText
android:id="@+id/editTextTextAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="number"
android:hint="Write number here..." />
</RelativeLayout>
I prefer in kotlin, but java code also works in order to understand.
Note: I didn't put code snippets cause it's clearer what I said above, but if it helps I'll put.
Any help is greatly appreciated, thanks a lot :)