Reputation: 1
I'm working on a school project, but I'm having a problem with a fragment transaction, hope you can help. When I click the textview, it jumps to another fragment and I tried to get the travel destination and departure information from the user. If you search in the moved fragment, the search list appears in the recycler view, and when you click the recycler item, the text of the destination becomes the text in the textview in the previous fragment. So I used add
, hide
, remove
instead of replace
to move fragments. But I had to receive two texts because I had to input the destination and departure. So I made two textviews move to the same fragment when clicked. But here's a problem. When the fragment was removed and added again, the information of the removed fragment remained.
So I want to know how to make the fragment information not remain.
This is my code
MainActivity
:
2 -> {
Log.d(TAG, "DispositionFragment -> DepartRegionFragment")
transaction
.setCustomAnimations(R.anim.horizon_enter,
R.anim.none,
R.anim.none,
R.anim.horizon_exit)
.add(R.id.fragment_container_view, departRegionFragment, "depart")
.hide(dispositionFragment)
.addToBackStack("depart")
.commit()
}
3 -> {
Log.d(TAG, "DepartRegionFragment -> DispositionFragment")
supportFragmentManager.popBackStack("depart", POP_BACK_STACK_INCLUSIVE)
transaction
.setCustomAnimations(R.anim.horizon_enter,
R.anim.none,
R.anim.none,
R.anim.horizon_exit)
.remove(departRegionFragment)
.show(dispositionFragment)
.commit()
}
DispositionFragment
is the fragment with textview and DepartregionFragment
is the fragment to search.
Upvotes: 0
Views: 204
Reputation: 11
You need to remove addToBackStack method call on your Fragment Transaction. Calling addToBackStack retains your fragment in the memory and only the view gets destroyed on moving away from that fragment.
Upvotes: 1