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)
{
....
Related
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
I was trying to change ImageView in one Fragment from another Fragment. But since this cannot be done without container, I have to create another view value with the inflater method. Then I need to return these view values with the inflater methods. How do I return two views?
code is written in a fragment:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.firstfragment, container, false)
val view2 = inflater.inflate(R.layout.secondfragment, container, false)
val imgPF: ImageView = view2.findViewById(R.id.imageView)
imgPF.setImageResource(R.drawable.image_29)
view.ButtonFromFirstFragment.setOnClickListener{
//Unimportant code
}
setHasOptionsMenu(true)
return view //Approximately here i need to return two views, but how?
//And can it be done somehow differently?
}
Basically you should not have 2 fragments within a fragment. Please double check that you are not confusing fragments with views.
If you want to have those 2 fragments in an activity and you are showing both of your fragments at the same time then you should have 2 separate fragments:
Fragment A:
(...)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.firstfragment, container, false)
view.ButtonFromFirstFragment.setOnClickListener{
//Unimportant code
}
return view
}
(...)
Fragment B:
(...)
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.secondfragment, container, false)
val imgPF: ImageView = view.findViewById(R.id.imageView)
imgPF.setImageResource(R.drawable.image_29)
return view
}
I want to create DialogFragment with custom ui. It's important for me to use custom layout made by myself.
But when returning one from onCreateView DialogFragment shows nothing. By the way when use same code in simple Fragment's child, view shows as expected.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
val context = requireActivity()
contentLoaderView = createContentLoaderView(context)
return contentLoaderView
}
private fun createContentLoaderView(context: Context): ContentLoaderView{
return ContentLoaderView(context).apply{
attachContentView(R.layout.dialog_product_order)
attachViewModel(viewLifecycleOwner, viewModel)
layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
}
}
While when i use the inflater all is nice.
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
return LayoutInflater.from(context).inflate(R.layout.dialog_product_order, container, false)
}
What am i doing wrong?
I have the following:
class SettingsFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_settings, container, false)
val auth = FirebaseAuth.getInstance()
signOutButton.setOnClickListener{
Log.i("TAG", "logout tapped")
auth.signOut()
startActivity(Intent(activity,LoginActivity::class.java))
}
}
}
The view loads, but when I tap the button to log the user out, I dont see a print out of the log and nor does the user logout and the view doesn't switch to the loginActivity().
My button:
<Button
android:id="#+id/signOutButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign out" />
As you have already returned the view the rest of the lines will never be called. It's unreachable. return be at the end of the function. Also, you will want to set the click listener after the view is created(onViewCreated) else you will get a NullPointerException.
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_settings, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val auth = FirebaseAuth.getInstance()
signOutButton.setOnClickListener{
Log.i("TAG", "logout tapped")
auth.signOut()
startActivity(Intent(activity,LoginActivity::class.java))
}
}
override the onViewCreated() function and write down your signout code there, Also initialize the signOutButton.
Lets suppose I have a fragment class like this one below named EmailSearchResultPageFragment().
class EmailSearchResultPageFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_email_search_result_page, container, false)
someFunctionThatRequiresViewFromOnCreate(view)
showPage(fragment)
return view
}
fun someFunctionThatRequiresViewFromOnCreate(view: View) {
view.DoSomethingWithView(view)
}
fun showPage(fragment: Fragment) {
fragmentManager!!.beginTransaction().addToBackStack(null).replace(R.id.container, fragment).commit()
}
}
If i want to unit test someFunctionThatRequiresViewFromOnCreate(:), and showPage(:) what would be the best practice ??
How can I possibly mock view and fragment that is being instantiated inside onCreate???!