Here is my code:-
class ACFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val id = "ac"
fb_ac.setOnClickListener {
val intent = Intent(this@ACFragment, AddFunction::class.java)
intent.putExtra("id", id)
startActivity(intent)
}
return inflater.inflate(R.layout.fragment_ac, container, false)
}
}
I want to transfer data from fragment to activity using Intent but I am unable to do it. Can Someone please help me?
CodePudding user response:
You can retrieve the id
string in the AddFunction
activity by calling the getStringExtra
method:
override fun onCreate(savedInstanceState: Bundle) {
...
val id = intent.getStringExtra("id") ?: ""
}
Also, you should change your onCreateView
declaration like this:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_ac, container, false)
val id = "ac"
view.findViewById<Button>(R.id.fb_ac).setOnClickListener {
val intent = Intent(requireContext(), AddFunction::class.java)
intent.putExtra("id", id)
startActivity(intent)
}
return view
}