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
- ui.main
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?
Answer
To retrieve the text contents of input_a_fragment_one
and input_b_fragment_two
in your TabbedActivity
Activity
, perform one of the following:
- Define a listener callback interface which is then implemented by your
Activity
. When the text in the relevantFragment
changes, invoke the callback to update a state variable in yourActivity
. - Retrieve a reference to the relevant
Fragment
by either utilizing afindFragmentBy*
method, or saving an instance of theFragment
during its initialization. You can then interact with theFragment
directly to retrieve the text you require.
Other methods exist, but one of these two options will suffice for your purposes.
Related Questions
- → should I choose reactjs+f7 or f7+vue.js?
- → Phonegap Android write to sd card
- → Local reference jquery script in nanohttpd (Android)
- → Click to navigate on mobile devices
- → How to allow api access to android or ios app only(laravel)?
- → Access the Camera and CameraRoll on Android using React Native?
- → React native change listening port
- → What is the default unit of style in React Native?
- → Google play market autocomplete icon
- → Warning: Each child in an array or iterator should have a unique "key" prop. Check the render method of `ListView`
- → Using Laravel with Genymotion
- → react native using like web-based ajax function
- → react native pdf View