Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Pic One

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
186 views
Welcome To Ask or Share your Answers For Others

1 Answer

You are capturing the old text view inside the activity. Once the fragments are replaced and recreted the old view object is not there anymore. what you see on the screen is a new text view with a new reference but the value you captured on the OnClick lambda doesn't match that new value. Try getting the view from the fragments with something like

currentlyLoadedFragment.view.findViewById(R.id.theTextViewID) // or similar

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...