android - 使用 Kotlin 导航到不同的活动
问题描述
我想使用导航控制器从一个活动导航到另一个活动。我还没有找到一个例子来说明如何做到这一点。Android 文档指示使用其中之一进行导航:
科特林
* Fragment.findNavController()
* View.findNavController()
* Activity.findNavController(viewId: Int)
爪哇
* NavHostFragment.findNavController(Fragment)
* Navigation.findNavController(Activity, @IdRes int viewId)
* Navigation.findNavController(View)
我正在使用科特林。出于某种奇怪的原因,Google 选择为 Kotlin 和 Java 创建单独的方法。
解决方案
绝对你需要使用:
findNavController().navigate(R.id.action_loginFragment_to_mainActivity)
就我而言,我必须使用:
val root = inflater.inflate(R.layout.fragment_home, container, false)
val btnUbicacion = root.findViewById<View>(R.id.btn_ubicacion) as Button
btnUbicacion.setOnClickListener{
activity!!.findNavController(R.id.nav_host_fragment).navigate(R.id.nav_gallery)
}
推荐阅读
- sql - SQL 按 Top 3 和其他对行进行分组。(按州和其他排名前 3 名的城市的收入)
- sql - 当 SQL 事务提交通过但客户端超时/失败时会发生什么
- ruby-on-rails - rails url_helper 将参数放在格式应该是的位置
- odata - 从特定电子邮件地址获取消息交换 sentdatetime 排序
- python - 如何将多种条件格式应用于数据框?
- unity3d - 创建类似的工具,如 Game Creator | Unity 编辑器自定义
- mongodb - 从 bash 脚本执行 mongodump 命令
- javascript - "SyntaxError: JSON.parse: unexpected character at line 1 column 2 of the JSON data".Django Channels
- android-motionlayout - 我可以在生产中使用运动布局吗
- symfony - 原则:多对多