androidandroid-fragmentskotlinandroid-viewpagerandroid-pageradapter

How to start activity to selected fragment?


how to start activity to selected fragment?

this is my fragment list and i need start actvity to topTygodnia() how do it?

    val adapter = ViewPageAdapter(supportFragmentManager)

    adapter.addFragmenr(Sciana(), "Posty")
    adapter.addFragmenr(Wydarzenia(),"Wydarzenia")
    adapter.addFragmenr(Konto_zarzadzanie(), "Ustawienia")
    adapter.addFragmenr(topTygodnia(), "top tygodnia")
    adapter.addFragmenr(TopMiesiaca(), "top miesiaca")
    adapter.addFragmenr(OstatnieWiadomosci(), "powiadomień")


    viewPager.adapter = adapter
    nawigacja.setupWithViewPager(viewPager)

Solution

  • topTygodnia fragment has index of 3 (zero-based index). So you can use ViewPagers currentItem property to set the currently selected page:

    viewPager.currentItem = 3
    

    Note: the good approach is to stick to class naming conventions: class name should start with a capital letter, consist of english words and have Camel Case Style. For example use WeekTopFragment instead of topTygodnia, AccountManagementFragment instead of Konto_zarzadzanie.