Comments (5)
This kind of behavior should not happen but, without seeing the entire implementation of your fragment and data management there is no way of telling what's wrong.
- when you leave the fragment and return to it, the fragment view is re-created?
- where are you populating your data?
- is the adapter being created again when returning to the fragment? or using the previous adapter with old data
- are you models implementing Diffable in a correct way for the DiffUtils to work in case the data is already there and re-assigned?
There are at least 10 more questions to try and understand the situation.
If you can post all the relevant code or even better upload a minimal project with the behavior above so I can resolve it if needed.
from oneadapter.
Thanks for the quick reply, with a custom adapter it works as usual, so I don't think it's a problem related to the Fragment lifecycle, here is the sample code:
Model ->
data class ResultItem(
override val uniqueIdentifier: Long,
val label: String,
val iconTint: Int
) : Parcelable, Diffable {
override fun areContentTheSame(other: Any): Boolean {
return other is ResultItem && label == other.label
}
}
Module->
class CheckmarkResultModule(parent: Fragment) :
ItemModule<ResultItem>() {
init {
config {
layoutResource = R.layout.item_checkmark_result
}
onBind { model, viewBinder, _ ->
viewBinder.dataBinding?.run {
setVariable(BR.resultModel, model)
lifecycleOwner = parent.viewLifecycleOwner
executePendingBindings()
}
}
}
}
Fragment logic->
class PlanInformationFragment : BaseFragment(R.layout.fragment_plan_information) {
private val args: PlanInformationFragmentArgs by navArgs()
private val binding by viewBinding(FragmentPlanInformationBinding::bind)
private lateinit var testItems: List<ResultItem>
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val planInformationArray =
requireContext().resources.getStringArray(R.array.plan_information)
testItems = planInformationArray.mapIndexed { index, string ->
ResultItem(index.toLong(), string, R.color.orange)
}
return super.onCreateView(inflater, container, savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
with(binding) {
with(resultsList) {
OneAdapter(this) { itemModules += CheckmarkResultModule(this@PlanInformationFragment) }.apply {
setItems(testItems)
}
}
choosePlan.setOnClickListener {
safeNavigation(
directions = PlanInformationFragmentDirections.actionOpenPlanDetails()
)
}
}
}
}
checkmark_result.xml ->
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<variable
name="resultModel"
type="com.example.ResultItem" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/checkmark"
android:layout_width="24dp"
android:layout_height="24dp"
android:importantForAccessibility="no"
android:src="@drawable/ic_check"
android:layout_marginTop="4dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@id/label"
app:tint="@color/light_green" />
<TextView
android:id="@+id/label"
style="@style/OnboardingResult"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="@{resultModel.label}"
app:layout_constraintStart_toEndOf="@id/checkmark"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="@tools:sample/lorem" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
Fragment layout ->
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/resultsList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintTop_toTopOf="parent"
tools:itemCount="3"
tools:listitem="@layout/item_checkmark_result" />
<Button
android:id="@+id/choosePlan"
style="@style/ButtonAction.Orange"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="42dp"
android:text="@string/onboarding_your_program_choose_plan"
app:layout_constraintTop_toBottomOf="@id/resultsList" />
</androidx.constraintlayout.widget.ConstraintLayout>
Navigation for the choosePlan button
<fragment
android:id="@+id/plan_information"
android:name="com.example.PlanInformationFragment"
android:label=""
tools:layout="@layout/fragment_plan_information">
<argument
android:name="title"
app:argType="reference" />
<action
android:id="@+id/action_open_plan_details"
app:destination="@id/plan_details"/>
</fragment>
Navigation code:
fun Fragment.safeNavigation(directions: NavDirections) {
NavHostFragment.findNavController(this).currentDestination?.getAction(directions.actionId)
?.apply {
findNavController().navigate(directions)
}
}
from oneadapter.
Checking your suggestions I think it's caused by Fragment being recreated during onbackpress on navigation architecture by default.
Because you process stuff on background it's noticeable the redrawing, while a normal adapter just makes it look like it was preserved.
I'll see what I can do about that. Thanks
from oneadapter.
If someone is having a similar issue check this link:
https://stackoverflow.com/questions/54581071/fragments-destroyed-recreated-with-jetpacks-android-navigation-components
from oneadapter.
Thanks for the information 👍🏼
from oneadapter.
Related Issues (20)
- Selection items selected other items HOT 2
- Support for StateRestorationPolicy HOT 4
- Scroll to first added element HOT 7
- Random crash in onCreate HOT 3
- use setItem, but the onBind method not be invoke HOT 12
- Question:Paging with Room Datasource HOT 2
- IndexOutOfBoundsException: Inconsistency detected HOT 2
- CalledFromWrongThreadException in 2.1.0 HOT 9
- Add drag & drop support
- Multiple ViewType HOT 9
- Can we call next page with Paging Module to load without scrolling HOT 5
- Preselected Items HOT 1
- Load more animation when submit empty list HOT 1
- It not a function to get the data from adapter. HOT 1
- Retry button HOT 2
- View Binding with Kotlin 1.6.0 not working HOT 5
- How do i filter the list ?
- You can't use interface as M : Diffable in intem module HOT 1
- select all
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from oneadapter.