How can I open a new Activity inside of a fragment when using a button?
I tried this
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
LogOut_btn.setOnClickListener {
//FirebaseAuth.getInstance().signOut()
val intent = Intent (this, Main::class.java)
startActivity(intent)
}
}
val intent = Intent doesn't seem to work in a fragment.
Any idea how I can start a new activity inside a fragment?
If you want to start a new instance of mFragmentFavorite , you can do so via an Intent . Intent intent = new Intent(this, mFragmentFavorite. class); startActivity(intent); If you want to start aFavorite instead of mFragmentFavorite then you only need to change out their names in the created Intent .
Best way of calling Activity from Fragment class is that make interface in Fragment and add onItemClick() method in that interface. Now implement it to your first activity and call second activity from there.
A fragment represents a modular portion of the user interface within an activity. A fragment has its own lifecycle, receives its own input events, and you can add or remove fragments while the containing activity is running.
Because Fragment
is NOT of Context
type, you'll need to call the parent Activity
:
val intent = Intent (getActivity(), Main::class.java)
getActivity().startActivity(intent)
or maybe something like
activity?.let{
val intent = Intent (it, Main::class.java)
it.startActivity(intent)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With