0

I am using recycle view with diffutil in my application. but while I rotating or comeback from another screen the adapter gets updated. why is this happening?.

Here My ViewModel

class FeedsViewModel() : ViewModel() {

    private val feedsRepository = FeedsRepository()
    val feedsLiveData: MutableLiveData<Resource<UserFeeds>> = MutableLiveData()
    init {
        val apiParams = HashMap<String, String>()
        apiParams["user_id"] = "1"
        getFeeds(apiParams,"123"
}
    fun getFeeds(apiParams: HashMap<String, String>, token: String) = viewModelScope.launch {
        feedsLiveData.postValue(Resource.Loading())
        val response = feedsRepository.getFeeds(apiParams, token)
        if (response.isSuccessful) {
            response.body()?.let { resultResponse ->
                feedsLiveData.postValue(Resource.Success(resultResponse))
            }
        } else {
            feedsLiveData.postValue(Resource.Error(response.message()))

        }
    }
}

I am using fragment to display it

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        binding.recyclerViewFeeds.adapter = feedsAdapter
        viewModel.feedsLiveData.observe(viewLifecycleOwner, Observer { response ->
            when (response) {
                is Resource.Success -> {
                    binding.progressBar.visibility = View.GONE
                    response.data?.let { userFeeds ->
                        feedsAdapter.differ.submitList(userFeeds.userPosts.toList())
                        binding.nooFeeds.visibility = View.GONE
                    }
                is Resource.Error -> {....}
                is Resource.Loading -> {....}
            }
        })
    }

and my adapter

class FeedsAdapter(private val context: Context, private val itemClickListener: FeedsItemCallBack) :
    RecyclerView.Adapter<FeedsAdapter.MyViewHolder>() {
    class MyViewHolder(val bindin: ItemViewFeedsBinding) : RecyclerView.ViewHolder(bindin.root) {
    }

    private val differCallback = object : DiffUtil.ItemCallback<UserPost>() {
        override fun areItemsTheSame(oldItem: UserPost, newItem: UserPost): Boolean {
            return oldItem.postId == newItem.postId
        }

        override fun areContentsTheSame(oldItem: UserPost, newItem: UserPost): Boolean {
            return oldItem == newItem
        }
    }

    val differ = AsyncListDiffer(this, differCallback)

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        var feedsItem = differ.currentList[position]
        holder.bindin.feedData = feedsItem;
        holder.bindin.executePendingBindings()
    }
    override fun getItemCount(): Int {
        return differ.currentList.size
    }
}

Is this implementation is correct?. Is this issue of ViewModel or adapter? please help. Thanks in advance

faizy
  • 504
  • 5
  • 17

1 Answers1

0
  1. you could distinguish the cases of your activity being created for the first time and being restored from savedInstanceState. This is done by overriding onSaveInstanceState and checking the parameter of onCreate.
  2. You could lock the activity in one orientation by adding android:screenOrientation="portrait" (or "landscape") to in your manifest.
  3. You could tell the system that you meant to handle screen changes for yourself by specifying android:configChanges="orientation|screenSize" in the tag. This way the activity will not be recreated, but will receive a callback instead (which you can ignore as it's not useful for you).

Personally, I'd go with (3). Of course if locking the app to one of the orientations is fine with you, you can also go with (2).

Nidheesh
  • 109
  • 9
  • yes I could handle the rotation issue by preventing the landscape. but when I get back from another screen this same happens. I need to fix this on that case. Also, I am using fragment, not activity(Single activity structure) – faizy Mar 02 '21 at 08:26
  • it's because the ViewModel observer triggering when you get back to the fragment – Nidheesh Mar 02 '21 at 08:37
  • https://bladecoder.medium.com/architecture-components-pitfalls-part-1-9300dd969808 – Nidheesh Mar 02 '21 at 08:39
  • viewModel.observeData().removeObservers(this); viewModel.observeData().observe(this, new Observer() { – Nidheesh Mar 02 '21 at 08:45