Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is it possible to set first coming page of viewPager2 with Intent in Kotlin?

I use ViewPager2 in Kotlin.

And according to the data value of DB, I change viewPager’s page index like below:

in DiaryActivity:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

        diaryDB
            .document("${userId}_${writeTime}")
            .get()
            .addOnCompleteListener { task ->
                if (task.isSuccessful) {
                    val document = task.result
                    if(document != null) {
                        if (document.exists()) {
                            viewPager.currentItem = 1
                        } else {
                            viewPager.currentItem = 0
                        }
                    }
                } else {
                    viewPager.currentItem = 0
                }
            }

But When I call DiaryActivity from other Activity by Intent, (If data value needs to go currentItem 1) It goes first to currentItem = 0 and slides to currentItem = 1

I don’t want screen shows first 0 and slides to 1, I want screen firstly shows 1.

Is it possible with viewPager2?

>Solution :

This should do the trick

viewPager.setCurrentItem(1, false) 
//smoothScroll – true to smoothly scroll to the new item, false to transition immediately
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading