我正在尝试使用导航控制器。我有一个底部导航视图。位于我的MainActivity上,它使用以下代码启动:
class MainActivity : AppCompatActivity() {
lateinit var navController : NavController
lateinit var logoHeaderImageView : ImageView
var toolbarMenu : Menu? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
logoHeaderImageView = findViewById(R.id.header_lakuin_image_view)
navController = Navigation.findNavController(this,R.id.nav_main_host_fragment)
setupBottomNavMenu(navController)
setupActionBar(navController)
}
fun setupBottomNavMenu(navController: NavController) {
NavigationUI.setupWithNavController(bottom_navigation_view,navController)
}
fun setupActionBar(navController: NavController) {
setSupportActionBar(main_activity_toolbar)
main_activity_toolbar.title = ""
val appBarConfiguration = AppBarConfiguration(
setOf(
// set some destination as the top hierarchy destination, to make the up button doesn't show.
R.id.destination_home,
R.id.destination_order,
R.id.destination_favourite,
R.id.destination_cart,
R.id.destination_profile
))
NavigationUI.setupWithNavController(main_activity_toolbar,navController,appBarConfiguration)
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_main_toolbar, menu)
toolbarMenu = menu
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
return item.onNavDestinationSelected(navController) || super.onOptionsItemSelected(item)
}
}
所以我想将数据从我的HomeFragment
(目的地主页)传递到OderFragment
(目的地顺序)。我通常使用bundle或safeArgs传递数据,如下面的代码:
var bundle = bundleOf("amount" to amount)
view.findNavController().navigate(R.id.confirmationAction, bundle)
但我不知道在哪里放置该代码,如果我想将数据从我的HomeFragment
传递给OderFragment
使用NavigationUI.setupWithNavController(bottom_navigation_view,navController)
时(或者,如果你使用navigation-ui-ktx
Kotlin扩展bottom_navigation_view.setupWithNavController(navController)
),你不能将任何自定义参数传递给目的地 - 全局导航的一个重要部分是它们总是将你带到同一状态的同一个屏幕。
通常,您应该将当前数量的数据与导航参数分开保存 - 无论是在持久数据库,SharedPreferences还是在流程死亡中存活的其他位置,允许用户继续他们正在做的事情,即使在重新启动他们的电话等
但是,如果必须为此使用导航参数,则可以提前为目标设置默认参数(即,每当您的金额更改时):
NavDestination orderDestination = navController.graph.findNode(R.id.destination_order)
orderDestination.addArgument("amount", NavArgument.Builder()
.setType(NavType.FloatType)
.setDefaultValue(amount)
.build())
之后,默认情况下,触发BottomNavigationView
的R.id.destination_order
会自动包含该参数以及新的amount
值。
就像那样:
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val bundle = Bundle()
bundle.putString("myname","Hussnain")
return when(item.itemId){
R.id.aboutFragment ->{
navController.navigate(R.id.aboutFragment,bundle)
return true
}else -> {
NavigationUI.onNavDestinationSelected(item,navController) || super.onOptionsItemSelected(item)
}
}
}
导航:
<fragment android:id="@+id/aboutFragment"
android:name="com.cinderellaman.general.ui.fragments
.AboutFragment"
android:label="about_fragment"
tools:layout="@layout/about_fragment">
<argument android:name="myname" app:argType="string"/>
</fragment>
分段:
val args: AboutFragmentArgs by navArgs()
name.text = args.myname
Post data:
Fragment fragment = new OderFragment();
Bundle bundle = new Bundle();
bundle.putString("key", "value");
fragment.setArguments(bundle);
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, fragment);
transaction.addToBackStack(null);
transaction.commit();
Receive data:
Bundle bundle = this.getArguments();
if (bundle != null) {
myInt = bundle.getString("key");
}
您可以在片段之间使用共享的ViewModel:
class SharedViewModel : ViewModel() {
val selected = MutableLiveData<Item>()
fun select(item: Item) {
selected.value = item
}
}
class MasterFragment : Fragment() {
private lateinit var itemSelector: Selector
private lateinit var model: SharedViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
model = activity?.run {
ViewModelProviders.of(this).get(SharedViewModel::class.java)
} ?: throw Exception("Invalid Activity")
itemSelector.setOnClickListener { item ->
// Update the UI
}
}
}
class DetailFragment : Fragment() {
private lateinit var model: SharedViewModel
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
model = activity?.run {
ViewModelProviders.of(this).get(SharedViewModel::class.java)
} ?: throw Exception("Invalid Activity")
model.selected.observe(this, Observer<Item> { item ->
// Update the UI
})
}
}
更多信息可以在这里找到:
https://developer.android.com/topic/libraries/architecture/viewmodel#sharing