Kotlin: open new Activity inside of a fragment

open fragment from activity kotlin
start fragment from activity
how to call activity from fragment in android
intent from activity to fragment
how to get context in fragment kotlin
kotlin navigate to fragment
how to link fragment to activity in android
convert activity to fragment kotlin

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 {
         val intent = Intent (this, Main::class.java)



val intent = Intent doesn't seem to work in a fragment.

Any idea how I can start a new activity inside a fragment?

I'm thankful for every answer I get and thank you for your help!

Because Fragment is NOT of Context type, you'll need to call the parent Activity:

 val intent = Intent (getActivity(), Main::class.java)

or maybe something like

    val intent = Intent (it, Main::class.java)

Start an activity from a fragment, mFragmentFavorite in your code is a FragmentActivity which is not the same thing as a Fragment . That's why you're getting the type mismatch. Also, you should  A fragment is defined in a Kotlin class. A fragment's UI is defined in an XML layout file. The AndroidTrivia app has a main activity and several fragments. Most of the fragments and their layout files have been defined for you. In this task, you create a fragment and add the fragment to the app's main activity. Step 1: Add a fragment class. In this step, you create a blank TitleFragment class. Start by creating a Kotlin class for a new fragment:

I believe it would be something like

activity?.let { callingActivity -> startActivity(Intent(callingActivity, Main::class.java)) }

You must use the calling activities context

Android Fragments Tutorial: An Introduction with Kotlin , In this Android Fragments with Kotlin tutorial you will learn the fundamental In this step, you're placing a <fragment> tag inside of the activity layout and Ask that FragmentManager to start a new transaction by calling  The difference between starting an Activity from a Fragment and an Activity is how you get the context, because in both cases it has to be an activity. From an activity: The context is the current activity (this) Intent intent = new Intent(this, NewActivity.class); startActivity(intent);

Your code is almost done, you just need to pass the fragment instance as the first parameter of Intent replace YourFragmentName with your fragment name after the @, bellow:

val intent = Intent (this@YourFragmentName.context, Main::class.java)

Look at this sample bellow:

class MyFragment: Fragment(){

    override fun onActivityCreated(savedInstanceState: Bundle?) {

        val intent = Intent (this@MyFragment.context, YOUR_NEXT_ACTIVITY_CLASS::class.java)

How to Open Activity from Fragment in Android, val intent = Intent doesn't seem to work in a fragment. Any idea how I can start a new activity inside a fragment? I'm thankful for every answer I  By including this in the XML, the FragmentManager knows to add this fragment to the activity automatically. Build and run. You will see the fragment: Adding a Fragment Dynamically. First, open activity_main.xml again and remove the <fragment> you inserted. Embrace the Zen of deleting code! You’ll replace it with the list of dog breeds.

In kotlin (fragment): write this code in setOnClickListener method of your Button or View

btn.setOnClickListener {
        val intent = Intent(activity, MainActivity::class.java)

[ KOTLIN ] OPEN FRAGMENT FROM ANOTHER FRAGMENT , This example demonstrates how do I call an activity method from a fragment in android.Step 1 − Create a new project in Android Studio, go to  Example of Android ViewPager Using Kotlin With Fragments . At first, we will create android application. Then, we will use viewPager in this application. 1. Creating New Project. Follow steps below to create new project. Please ignore the steps if you have already created a new application.

Also an option (for kotlin). In onCreateView set onClickListener for a button:

button.setOnClickListener {
    requireActivity().run {
        startActivity(Intent(this, MainActivity::class.java))
        finish() // If activity no more needed in back stack

Kotlin: open new Activity inside of a fragment, I am new to kotl Android multiple fragments in one activity example kotlin. Back to our Kotlin activity code, we'll start implementing our first logic inside  Quick intro to the Kotlin features we are about to use: Extension function is a way to add new functions (or properties) to an existing class even if the class is from a library or SDK. Inside the

How to call an activity method from a fragment in Android App?, kotlin.Any. ↳, android.app.Fragment A Fragment is closely tied to the Activity it is in, and can not be used apart from one. Though An activity's layout XML can include <fragment> tags to embed fragment instances inside of the layout. Create a new instance of a Fragment with the given class name. You can replace the fragment using FragmentTransaction on button click. Something like this: Fragment someFragment = new SomeFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); transaction.replace(R.id.fragment_container, someFragment ); // give your fragment container id in first parameter transaction.addToBackStack(null); // if written, this transaction

One activity to another activity in kotlin, Developer Guides; Fragments; Activity Lifecycle; Configuration Changes Kotlin |Java Activity#startActivity method is used to start a new activity, which will be Sets information describing the task with this activity for presentation inside the  Kotlin is known the official language for android app development, so we will develop Android Fragment example app using Kotlin. Creating a Fragment To create a fragment, you must create a subclass of Fragment (or an existing subclass of it).

Fragment, How to Open Activity from Fragment in Android - Navigation Drawer - Duration: 6:42. Oum Saokosal 51,440 views

  • getActivity().startActivity(intent)
  • Thank you for your suggestion but the app still crashes when I open the activity where the fragment is shown starts
  • What's the exception you're getting? maybe you forgot to declare the activity in the manifest?
  • I am the dumbest person in the universe. I have 3 fragments and all the time I wrote in the wrong one. Really sorry for my stupidity. Thank you for helping me
  • nah. you're just another person in the universe. 🖤
  • Thank you for your effort but the app still crashes
  • first of all I want to thank you for trying to help me, but when I put my fragments name in there the Intent stops working and gets a red underline.
  • sorry its still crashing. Should I even ride this in override fun onViewCreated or in something else. Maybe I shouldn't do that with a fragment
  • I added another sample