Search code examples
androidkotlinandroid-viewpager2

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:

        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