2

I am working in ViewPager 2 with Paging 3 library in my application. When I am opening my view pager screen again and again i.e. Open the screen and close it and again so on and so. The first few times it opens the correct page number but sometimes it gives me the wrong page number to open when doing fast open and close. I asked a similar question and it solve the issue, but something is similar issue came and 100% confident this issue is related to ViewPager 2

class activity : BaseActivity() {

    private val viewModel: ViewPagerViewModel by inject()
    private var adapter = createAdapter()
    private lateinit var binding: ViewPagerActivityLayoutBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ViewPagerActivityLayoutBinding.inflate(layoutInflater)
        setContentView(binding.root)
        setUpRepoAndAdapter()
    }

    private fun setUpRepoAndAdapter() {
        val viewRepository = ViewRepository()
        lifecycleScope.launchWhenCreated {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                viewModel.createRepositoryData(viewRepository).collect { data ->
                    adapter = createAdapter()
                    binding.viewViewpager.adapter = adapter
                    adapter.submitData(data)
                }
            }
        }
    }

    private fun createAdapter(): ViewPagerAdapter {
        return ViewPagerAdapter(action = {
            launchScreen()
        })
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
         if (requestCode == DAILY_VIEW) {
            data?.let { intent ->
                (intent.extras?.get(BUNDLE_KEY) as? Date)?.let { clickedDate ->
                    viewModel.initialDate = clickedDate
                    setUpRepoAndAdapter()
                }
            }
        }
    }
}

I am adding a screencast of my original application. I am clicking on 1st Dec. Date and it's opening as 2nd Dec sometime and sometimes opening correct date. I am not understanding why this is happening this. The above stack overflow link inside has my GitHub sample project link.

Ramesh R
  • 7,009
  • 4
  • 25
  • 38
Kotlin Learner
  • 3,995
  • 6
  • 47
  • 127
  • The git repo you mentioned doesn't showcase the issue, provide more input, either update the git repo, or post the logic you implemented in your `ViewPagerAdapter` and `launchScreen` – Muhannad Fakhouri Jan 08 '22 at 09:42
  • @MuhannadFakhouri the github link is I added the sample of whole project. The video I attached is my school project. I can't share that. That's why I added my sample project. – Kotlin Learner Jan 08 '22 at 10:50
  • @MuhannadFakhouri can you see this [issue](https://stackoverflow.com/q/69755212/11560810) it both are same – Kotlin Learner Jan 08 '22 at 10:52
  • I cannot really see how both issues are related, anyways I've posted an answer for the other one, to get this one fixed, you need to provide a sample where it can be observed. – Muhannad Fakhouri Jan 08 '22 at 21:49
  • @MuhannadFakhouri I used the same logic in every where. The core logic is same as this logic in this issue both are link – Kotlin Learner Jan 08 '22 at 23:35

0 Answers0