Giter Club home page Giter Club logo

Comments (5)

idanatz avatar idanatz commented on May 29, 2024

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.

strangel3t avatar strangel3t commented on May 29, 2024

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.

strangel3t avatar strangel3t commented on May 29, 2024

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.

strangel3t avatar strangel3t commented on May 29, 2024

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.

idanatz avatar idanatz commented on May 29, 2024

Thanks for the information 👍🏼

from oneadapter.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.