Trying to sent data from activity to fragment - android

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
}

Related

erorr when trying to reach view with findviewbyId

class main_fragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main_fragment, container, false)
val viewPager = view.findViewById<ViewPager>(R.id.viewPager)
viewPager.adapter = PageAdapter(childFragmentManager)
val tabLayout = view.findViewById<TabLayout>(R.id.tabLayout)
tabLayout.setupWithViewPager(viewPager)
}
}
i am getting an erorr Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type View?
can anyone help me?
class main_fragment : Fragment() {
val view: View
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_main_fragment, container, false)
val viewPager = view.findViewById<ViewPager>(R.id.viewPager)
viewPager.adapter = PageAdapter(childFragmentManager)
val tabLayout = view.findViewById<TabLayout>(R.id.tabLayout)
tabLayout.setupWithViewPager(viewPager)
return view
}
}
override onViewCreated method
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState)
and then use view parameter from this function

Difference between onCreateView and onViewCreated in Fragment - Kotlin

I know this question has been asked many times but just wanted to have some more clarity.
So I wanted to create a simple fragment with a button that should change the fragment when clicked - very simple and basic one.
hence I create a function, and called it on onCreateView. Nothing happeded.
Then I created onViewCreated after onCreateView and called the same function in it.
It Worked.
My Question is What exactly made it work ?
here is the code
class homeFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
hello()
}
fun hello()
{
val button = view?.findViewById<Button>(R.id.button_login)
button?.setOnClickListener{
val action = homeFragmentDirections.actionHomeFragmentToLoginFragment()
findNavController().navigate(action)
Toast.makeText(context,"wao",Toast.LENGTH_LONG).show()
}
}
}
As per your comments, you did something like this:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_home, container, false)
hello() // the method has already returned a View, so this call is never reached.
}
So, when you call hello() in onCreateView after the return statement,
the method has no effect because of the return statement.
Since onViewCreated is called after the onCreateView,
the underlying View is no more null in your hello function.
If you still want to use onCreateView, you can do something like this:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val fragmentView = inflater.inflate(R.layout.fragment_home, container, false)
hello(fragmentView)
return fragmentView
}
fun hello(buttonHolderView: View?) {
val button = buttonHolderView?.findViewById<Button>(R.id.button_login)
button?.setOnClickListener {
val action = homeFragmentDirections.actionHomeFragmentToLoginFragment()
findNavController().navigate(action)
Toast.makeText(context, "wao", Toast.LENGTH_LONG).show()
}
}
You need to inflate the view before calling the function:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val fragmentHome = inflater.inflate(R.layout.fragment_home, container, false)
hello(fragmentHome)
return fragmentHome;
}
Change your hello function:
fun hello(view: View)
{
....

How to tranfer data from fragment to activity in Android using Kotlin?

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
}

Numberpicker send data to HomeFragment (Fragment to Fragment)

I have app who have viewpagers and in inside is numberpicker. I try to send this data to my HomeFragment but crash my app. This is my source code. What i do wrong?
Create Interface for communication
interface Communicator {
fun passData(numberpicker: NumberPicker) {
}
This is my fragment with numberpicker
lateinit var comm: Communicator
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_period_length, container, false)
view.number_picker_period?.minValue = 21
view.number_picker_period?.maxValue = 35
view.number_picker_period?.wrapSelectorWheel = true
val viewPager = activity?.findViewById<ViewPager2>(R.id.viewPager)
view.next_txt_period.setOnClickListener {
viewPager?.currentItem = 1
comm.passData(view.number_picker_period)
}
return view
}
This is MainActivity
val fragment1 = DaysLength()
supportFragmentManager.beginTransaction().replace(R.id.content_id, fragment1).commit()
}
override fun passData(numberpicker: NumberPicker) {
val bundle = Bundle()
bundle.putString("input_txt", number_picker_period.toString())
val transaction = this.supportFragmentManager.beginTransaction()
val fragment2 = HomeFragment()
fragment2.arguments = bundle
}
My HomeFragment
var inputtext: String? = ""
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val view = inflater.inflate(R.layout.fragment_home, container, false)
inputtext = arguments?.getString("input_txt")
view.output_numberpicker.text = inputtext
return view
What i do wrong?
Error
Unable to start activity ComponentInfo{com.example.oapp/com.example.oapp.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f080067 (com.example.oapp:id/content_id) for fragment DaysLength{aa77410}
your main activity is crashing on this line as it doesn't know what the view "number_picker_period" is
bundle.putString("input_txt", number_picker_period.toString())
try accessing the number picker through the parameter in the function called "numberPicker", Or alternatively, change your passData interface function to return the string value instead of the entire numberPicker

Passing data from db_helper Activity to Fragment in kotlin

for Sending Data:
val u = Users()
val b = Bundle()
b.putStringArrayList("name01", name01)
u.arguments=b
for Receiving Data:
override fun onCreateView(inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
strtext = arguments!!.getStringArrayList("name01")
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_users, container, false)
}
output: kotlin.KotlinNullPointerException
How should I get the data?
Is there is another Method?

Categories

Resources