Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to send argument when switching tab #235

Open
mochadwi opened this issue Sep 14, 2020 · 0 comments
Open

How to send argument when switching tab #235

mochadwi opened this issue Sep 14, 2020 · 0 comments

Comments

@mochadwi
Copy link

mochadwi commented Sep 14, 2020

in the getRootFragment(index: Int) method, we can only declare a DestinationFragment.newInstance("hardcoded value1") without having to know how to send argument, e.g (from a deeplink):

fun iniFragNav() {
 val fragments = listOf(Fragment(), Fragment(), DestinationFragment())
 val deeplink = "example://destination/tab2"
 val tabName = Uri.from(deeplink).path.split[1] // a local variable

 bottomNavigation.setOnTabSelected { position: Int ->
   if (position == 2) DestinationFragment.newInstance(tabName)
 }
}
// DestinationFragment.kt

class DestinationFragment : Fragment() {

   companion object {
       fun newInstance(tabName: String): DestinationFragment {
             return DestinationFragment().putArgs { // an anko extension
                  putString("tab", tabName)
            }
       }
  }

 // e.g: init viewpager and set current item based on argument
 fun initViewPager() {
     viewpager.currentItem = when (arguments?.getString("tab")) {
          "tab1" -> 1
          "tab2" -> 2
          else -> 0
    }
 }
}

any reference to achieve the above approach? or maybe a workaround to send an argument via FragNavController.

our usecase is for a deeplink scheme to go into specific tab in DestinationFragment with ViewPager

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant