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?
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
}
Related
In Main
override fun pastDada(name: String) {
val bundle = Bundle()
bundle.putString("USER_NAME", "DDDDDDDD")
val transction = this.supportFragmentManager.beginTransaction()
val profileFragmant = fragment_profile()
profileFragmant.arguments = bundle
transction.replace(R.id.container,profileFragmant)
}
In Frafment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
var view = inflater.inflate(R.layout.fragment_profile2, container, false)
name = arguments?.getString("USER_NAME")
Log.e("name", arguments?.getString("USER_NAME").toString())
Log.e("name", name.toString())
view.tv_name.text = name
return view
}
the result of Log.e("name", name.toString()) is always null and I dont know why
maby its becuse my arguments is empty?
and if it is,how am I supose to slove it?
Thanks
change your code like this
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
var view = inflater.inflate(R.layout.fragment_profile2, container, false)
val bundle = arguments
val name = bundle!!.getString("USER_NAME")
Log.e("name", name.toString())
view.tv_name.text = name
return view
}
Im trying to pass data between two fragments but i have an error with arguments in execution when i launch fullFragment
i updated my code
Can you help me ?
architecture :
ListeFragment (sender) :
class ListeFragment : Fragment() {
companion object {
fun newInstance(s: String) = ListeFragment()
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val layout = inflater.inflate(R.layout.liste_fragment, container, false)
val photo1 = layout.findViewById<ImageView>(R.id.imageButton1)
val model = ViewModelProviders.of(this).get(ListeViewModel::class.java)
imageButton1.setOnClickListener {
val newFragment = FullFragment()
val args = Bundle()
args.putString("key1", "data")
newFragment.arguments = args
Navigation.findNavController(imageButton1).navigate(R.id.versFullFragment);
}
FullFragment (receiver):
class FullFragment : Fragment() {
companion object {
fun newInstance() = FullFragment()
}
private lateinit var viewModel: FullViewModel
override fun onCreate( savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
var value1 = getArguments()?.getString("key1")
Log.v("value" , value1.toString())
Toast.makeText(context, value1.toString(), Toast.LENGTH_SHORT).show()
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val layout = inflater.inflate(R.layout.full_fragment, container, false)
return layout
}
I don't know what "newFragment.arguments = args" do because in FullFragment class I don't see any variable with that name, however you have to use "Fragment.setArguments(Bundle)" to set Fragment Arguments from the first one. Then in the Full you will have a correct result from "FullFragment.getArguments()" method.
PS: don't forget that the Context could be NULL in Fragment class and it should be handled in proper way.
I'm new to Kotlin and I'm just trying to figure out how to use spinners in a fragment.
So I guess the code below is how I should initialize the spinner but how do I point this to my premade strings in R.array and how do I capture the selections on button click. Thanks.
class paychart : Fragment() {
private lateinit var paychartview: View
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
paychartview = inflater.inflate(R.layout.fragment_paychart, container, false)
}
val yearspinner = paychartview.findViewById<Spinner>(R.id.spinneryear)
}
Use ArrayAdapter.createFromResource
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val paychartview = inflater.inflate(R.layout.fragment_paychart, container, false)
val spinner = paychartview.findViewById<Spinner>(R.id.spinneryear)
spinner?.adapter = ArrayAdapter.createFromResource(activity, R.array.string_array, android.R.layout.simple_spinner_item) as SpinnerAdapter
spinner?.onItemSelectedListener = object :AdapterView.OnItemSelectedListener{
override fun onNothingSelected(parent: AdapterView<*>?) {
//..
}
override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
val item = parent?.getItemAtPosition(position).toString()
//..
}
}
return paychartview
}
Its easy to link texts on MainActivity.kt file but I can't figure out how to link the same on a Fragment.kt file cause the functions are different.
This is the code for MainActivity.kt
class MainActivity : AppCompatActivity() {
var text: TextView? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
text = findViewById(R.id.textLink)
text?.setOnClickListener({
var click = Intent (this#MainActivity, LinkText:: class.java)
startActivity(click)
})
}
}
I want to link the text in HomeFragment.kt which looks like this
class HomeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater.inflate(R.layout.fragment_home, null)
}
}
I need the code for the same.
Suppose that in fragment_home.xml you have a TextView with id textLink. Here is code how to "link" text on the HomeFragment.
class HomeFragment : Fragment() {
var text: TextView? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_home, container, false)
text = view.findViewById(R.id.textLink)
text?.setOnClickListener {
var click = Intent(requireActivity(), LinkText::class.java)
startActivity(click)
}
return view
}
}
class HomeFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
// Inflate the layout for this fragment
val v = inflater.inflate(R.layout.fragment_home, container, false)
val fab = v.findViewById(R.id.fab) as FloatingActionButton
fab.setOnClickListener {
val blankFragment = BlankFragment()
val manager = childFragmentManager
manager.beginTransaction().replace(R.id.frame_container, blankFragment, blankFragment.tag).commit()
// System.out.println("You have reached the floating action button")
}
return v
}
}
Getting a no view found error. I may have issues with the R.id.frame_content but Kotlin doesn't immediately identify all id values...
This may not be the best way and I apologize for the horrible formatting I does this while answer out on my phone and it's hard to get it perfect. Anyways In the activity that holds your fragments, such as MainActivity.kt add...
supportFragmentManager.beginTransaction.add(R.id.fragment_home, HomeFragment.newInstance, "HomeFragment").commit()
In your HomeFragment.kt change the following.
class HomeFragment: Fragment(){
companion object {
fun newInstance(): HomeFragment{
return HomeFragment() }
fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater!!.inflate(R.layout.fragment_home, container, false)
val activity = activity
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val clickListener = View.OnClickListener { view ->
when (view.getId()) {
R.id.fab -> NextFragment()
}
}
fab.setOnClickListener(clickListener)
}
NextFragment(){
val fragManager = activity?.supportFragmentManager
fragManager?.beginTransaction()?.replace(R.id.frame_container, BlankFragment.newInstance(), "blankFragment")?.commit()
}
Make sure you make the same changes to BlankFragment.kt
class BlankFragment: Fragment(){
companion object {
fun newInstance(): BlankFragment{
return BlankFragment() }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater!!.inflate(R.layout.fragment_blank, container, false)
val activity = activity
return view
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
//Do some stuff with your views in BlankFragment here
}
}