I just create a simple app. I have MainActivity, FragmentOne and FragmentTwo. In picture you can see I can go FragmentOne and FragmentTwo anytime. And with "Update Textview" button I can change textview in fragmentOne. Example when I start the app, I just go fragmentone and go two and go again to one etc... And when I am in FragmentOne, I click to update button and the textview changes. There is no problem. And I go to FragmentTwo and come back FragmentOne and try to press Update button again, nothing changes.
In brief you can see in picture the textview "FRAGMENT ONE CREATED", I can change it for once, i can't change again. I didn't change Fragment's codes. This is my MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
goFragmentOne.setOnClickListener {
val myAction = supportFragmentManager.beginTransaction()
myAction.replace(R.id.myFrameLayout,FragmentOne()).commit()
}
goFragmentTwo.setOnClickListener {
val myAction = supportFragmentManager.beginTransaction()
myAction.replace(R.id.myFrameLayout,FragmentTwo()).commit()
}
btnUpdate.setOnClickListener {
textViewOne.text = "UPDATED"
}
}
}
This is my MainActivity XML:
<FrameLayout>
<Button
android:id="@+id/goFragmentOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO FRAGMENT ONE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/goFragmentTwo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GO FRAGMENT TWO"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<Button
android:id="@+id/btnUpdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE TEXTVIEW"
app:layout_constraintBottom_toTopOf="@+id/goFragmentOne"
app:layout_constraintStart_toStartOf="parent" />
<FrameLayout
android:id="@+id/myFrameLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/btnUpdate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</FrameLayout>