0

My favourites fragment does not display the data given correctly unless the onCreateView function is called. This means that the fragment needs to be deleted and created again. Is there a way to appropriately refresh the fragment in the onResume method?

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val fragment = this

        _binding = FragmentFavouritesBinding.inflate(layoutInflater)
        binding.recyclerView.apply{
            layoutManager = GridLayoutManager(activity,2)
            adapter= CardAdapter(favouriteList, fragment)
        }
        return binding.root
    }
override fun onResume() {
        super.onResume()

        orderFavourites()
        sortFavourites()
        
        if(favouriteList.isEmpty()){
            Toast.makeText(this.context,"No Favourites Found", Toast.LENGTH_SHORT).show()
        }
        else{
            Toast.makeText(this.context,"resumed", Toast.LENGTH_SHORT).show()
        }
    }

The favouriteList is updated in another activity and I have ensured that it contains the correct values. It displays as intended after deleting and creating the fragment once more.

  • how are you updating `favouriteList` from the activity? – Praveen Feb 07 '23 at 04:48
  • You can just extract the code where you set the adapter into it's own method and call that manually when you need to "refresh" your data. – Robin Feb 07 '23 at 07:48
  • @Praveen favouriteList is a mutable list inside a kotlin class I created. I simply add objects to that list and I know it holds the correct values as intended currently. – lunderhill Feb 08 '23 at 05:06
  • @Robin I don't quite understand what you mean? I have attempted to use the same code that's in the onCreateView inside the onResume and that doesn't appear to work. – lunderhill Feb 08 '23 at 05:08

0 Answers0