Ad

Saving Fragment EditText Contents Via The Main Activity FAB Button OnClickListener

I have a tabbed scrolling activity with several fragments as follows:

  • com.example.myapp

    • ui.main
      • 1Fragment.kt
      • 2Fragment.kt
    • TabbedActivity.kt
    • DB_Constructors.kt
  • res...layout

    • activity_tabbed.xml
    • fragment_1.xml
    • fragment_2.xml

In activity_tabbed.xml I have a FAB button:

    <com.google.android.material.floatingactionbutton.FloatingActionButton
            android:id="@+id/fab"
            android:.../>

In fragment_1.xml, I have an EditText element:

<EditText android:id="@+id/input_a_fragment_one"
          android:.../>

In fragment_2.xml, I have an EditText element:

<EditText android:id="@+id/input_b_fragment_two"
          android:.../>

I want to write data inputted into both fragments EditText into a Firebase Realtime DB when the fab button is pressed.

In DB_Constructors.kt I have created the following class:

class SubmissionDetails(val line1_fragement1: String, line1_fragment2: String)

I have undertaken the following in TabbedActivity.kt:

class TabbedActivity : AppCompatActivity() {
    val ref  = FirebaseDatabase.getInstance().getReference("userData")
    lateinit var input_a_fragment_one: EditText
    lateinit var input_b_fragment_two: EditText
    lateinit var saveButton: Button
    public val currentUser = FirebaseAuth.getInstance().currentUser

    val fab: FloatingActionButton = findViewById(R.id.fab)

    fab.setOnClickListener { view ->
        Snackbar.make(view, "A message", Snackbar.LENGTH_LONG
             .setAction("Action", null).show()
        saveAccount()
    }
}

fun saveAccount(){

    val userId = currentUser?.uid ?: return

    val submissionDetails = SubmissionDetailsPrep(inputA, inputB)

    ref.child(userId).setValue(submissionDetails).addOnCompleteListener{

        //On Complete Activities...

    }

}

in 1Fragment.kt I have the following:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val inputA = input_a_fragment_one.text.toString().trim()
}

in 2Fragment.kt I have the following:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val inputB = input_b_fragment_two.text.toString().trim()
}

I am aware that certain elements are located incorrectly, but cannot figure out why? At present, inputA & inputB aren't available in the main TabbedActivity file. How do I make these available?

Ad

Answer

To retrieve the text contents of input_a_fragment_one and input_b_fragment_two in your TabbedActivityActivity, perform one of the following:

  • Define a listener callback interface which is then implemented by your Activity. When the text in the relevant Fragment changes, invoke the callback to update a state variable in your Activity.
  • Retrieve a reference to the relevant Fragment by either utilizing a findFragmentBy* method, or saving an instance of the Fragment during its initialization. You can then interact with the Fragment directly to retrieve the text you require.

Other methods exist, but one of these two options will suffice for your purposes.

Ad
source: stackoverflow.com
Ad