SafeArgs是gradle插件,可让您将数据传递到目标UI组件。它生成简单的对象和构建器类,以进行类型安全的导航以及对任何关联参数的访问。强烈建议使用Safe Args导航和传递数据,因为它可以确保类型安全。下面提供了一个示例视频,以使您对我们在本文中将要做的事情有个大概的了解。请注意,我们将使用Kotlin语言实施此项目。
分步实施
步骤1:创建一个新项目
要在Android Studio中创建新项目,请参阅如何在Android Studio中创建/启动新项目。请注意,选择Kotlin作为编程语言。
第2步:添加依赖项
在build.gradle(项目)内部,在依赖项下添加以下代码。
dependencies {
classpath “androidx.navigation:navigation-safe-args-gradle-plugin:2.3.2”
}
在build.gradle(应用程序)内部,添加以下代码,然后单击立即同步
plugins {
id ‘com.android.application’
id ‘kotlin-android’
id ‘kotlin-android-extensions’
id “androidx.navigation.safeargs.kotlin”
}
步骤3:创建两个新的片段
在本文中,我们将要从一个片段发送数据,并在另一个片段中接收数据。因此,首先,创建两个片段。要创建一个新的片段:
Project Name (right click) -> new -> Fragment -> Fragment (Blank)
将打开一个对话框。在“片段名称”中编写“ Registration” ,在“片段布局名称”中编写“ fragment_registration” 。以类似的方式创建另一个片段,其片段名称为Detail ,片段布局名称为fragment_detail 。
步骤4:创建两个片段的XML布局
转到Fragment_registration.xml文件,并参考以下代码。下面是Fragment_registration.xml文件的代码。这是用于应用程序中的基本布局。
XML
XML
Kotlin
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
data class User(
val name : String ="",
val email : String= "",
val password : String =""
) : Parcelable
XML
XML
Kotlin
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.geeksforgeeks.navargsexample.databinding.FragmentRegistrationBinding
class Registration : Fragment() {
private var _binding: FragmentRegistrationBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentRegistrationBinding.inflate(inflater, container, false)
val view = binding.root
// call onClick on the SendButton
binding.buttonSend.setOnClickListener {
val name = binding.etName.text.toString()
val email = binding.etEmail.text.toString()
val password = binding.etPassword.text.toString()
// create user object and pass the
// required arguments
// that is name, email,and password
val user = User(name,email, password)
// create an action and pass the the required user object to it
// If you can not find the RegistrationDirection try to "Build project"
val action = RegistrationDirections.actionRegistrationToDetails(user)
// this will navigate the current fragment i.e
// Registration to the Detail fragment
findNavController().navigate(
action
)
}
return view
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Kotlin
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.navArgs
import com.geeksforgeeks.navargsexample.databinding.FragmentDetailsBinding
class Details : Fragment() {
private var _binding: FragmentDetailsBinding? = null
private val binding get() = _binding!!
// get the arguments from the Registration fragment
private val args : DetailsArgs by navArgs()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentDetailsBinding.inflate(inflater, container, false)
val view = binding.root
// Receive the arguments in a variable
val userDetails = args.user
// set the values to respective textViews
binding.tvName.text = userDetails.name
binding.tvEmail.text = userDetails.email
binding.tvPassword.text = userDetails.password
return view
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
转到Fragment_detail.xml文件,并参考以下代码。下面是Fragment_detail.xml文件的代码。
XML格式
步骤5:建立新的Kotlin类别
创建一个新的User.kt类,我们将使用具有名称,电子邮件,密码的自定义通用“ User”数据来传递给另一个片段。
科特林
import android.os.Parcelable
import kotlinx.android.parcel.Parcelize
@Parcelize
data class User(
val name : String ="",
val email : String= "",
val password : String =""
) : Parcelable
步骤6:建立新的导览图
res (right click) -> new -> Android resource file
在对话框中,将文件名命名为nav_graph,然后选择资源类型作为“ Navigation ”。然后单击确定。现在打开刚刚创建的nav_graph.xml文件,单击新的目标图标,然后选择两个片段。使Fragment_registration作为主目录。创建从fragment_registration到fragment_detail的操作,并在Fragment_detail的参数中传递User参数。其XML代码如下。
XML格式
步骤7:使用activity_main.xml文件
转到activity_main.xml文件,并参考以下代码。以下是activity_main.xml文件的代码。此布局包含一个“框架布局”和一个片段,它将充当我们之前创建的片段的主机。注意fragment标签中的navGraph标签。
XML格式
步骤8:使用Registration.kt文件
转到Registration.kt文件,并参考以下代码。下面是Registration.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。
科特林
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.findNavController
import com.geeksforgeeks.navargsexample.databinding.FragmentRegistrationBinding
class Registration : Fragment() {
private var _binding: FragmentRegistrationBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentRegistrationBinding.inflate(inflater, container, false)
val view = binding.root
// call onClick on the SendButton
binding.buttonSend.setOnClickListener {
val name = binding.etName.text.toString()
val email = binding.etEmail.text.toString()
val password = binding.etPassword.text.toString()
// create user object and pass the
// required arguments
// that is name, email,and password
val user = User(name,email, password)
// create an action and pass the the required user object to it
// If you can not find the RegistrationDirection try to "Build project"
val action = RegistrationDirections.actionRegistrationToDetails(user)
// this will navigate the current fragment i.e
// Registration to the Detail fragment
findNavController().navigate(
action
)
}
return view
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
步骤9:使用Details.kt文件
转到Details.kt文件,并参考以下代码。下面是Details.kt文件的代码。在代码内部添加了注释,以更详细地了解代码。
科特林
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.navigation.fragment.navArgs
import com.geeksforgeeks.navargsexample.databinding.FragmentDetailsBinding
class Details : Fragment() {
private var _binding: FragmentDetailsBinding? = null
private val binding get() = _binding!!
// get the arguments from the Registration fragment
private val args : DetailsArgs by navArgs()
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
_binding = FragmentDetailsBinding.inflate(inflater, container, false)
val view = binding.root
// Receive the arguments in a variable
val userDetails = args.user
// set the values to respective textViews
binding.tvName.text = userDetails.name
binding.tvEmail.text = userDetails.email
binding.tvPassword.text = userDetails.password
return view
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
Note: In this example, we don’t have to write any code inside ActivityMain.kt
输出:
Github回购在这里。