L. Busekrus
L. Busekrus

Reputation: 412

Kotlin: open new Activity inside of a Fragment

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?

Upvotes: 22

Views: 66228

Answers (15)

Noelia
Noelia

Reputation: 4144

If you only use activity returns just an Activity instance. This can be any activity that embeds your fragment so in some cases you can get FragmentActivity instead your parent activity. Use this to make sure you are getting the correct one:

(activity as? YourParentActivity)?.let{
    val intent = Intent (it, Main::class.java)
    it.startActivity(intent)
}

Upvotes: 4

nyx69
nyx69

Reputation: 959

val intent = Intent (getActivity(), NextActivity::class.java)
getActivity()?.startActivity(intent)

This will do the job.

getActivity() as it's casted from a Fragment and getActivity()? to avoid NPE

Hope it helps :)

Upvotes: 4

Ahmad Abdullah
Ahmad Abdullah

Reputation: 141

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

Upvotes: 0

Ahmed Shehatah
Ahmed Shehatah

Reputation: 11

 override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment 

val view = inflater.inflate(R.layout.YourFragmentLayout, container, false)

    view.button.setOnClickListener {
        val intent = Intent(requireContext(), YourActivity::class.java)
        startActivity(intent)

    }


    return view

}

Upvotes: -1

Soumen Das
Soumen Das

Reputation: 1302

val  intent = Intent(activity, AttachmentActivity::class.java)
            activity!!.startActivity(intent)
             activity!!.finish()

Upvotes: 1

Tim
Tim

Reputation: 1826

Here is the solution I found while trying to get something similar to work.

view.findViewById<Button>(R.id.button_second).setOnClickListener {
  val i = Intent(activity, ClassNameOfActivityIWantToGoTo::class.java)
  activity?.startActivity(i)
}

Upvotes: 1

ITgrepNet
ITgrepNet

Reputation: 21

For me i just did this and it worked

val intent = Intent(AppName.applicationContext(), YourAppName::class.java)
activity?.startActivity(intent)

You can add this code into your fragment

AppName

This is the Application name which holds global companion object like context

Upvotes: 2

Vlad
Vlad

Reputation: 169

You can do smth like this in kotlin

YourButton.setOnClickListener{
            requireActivity().run{
                startActivity(Intent(this, NeededActivity::class.java))
                finish()
            }
        }

Upvotes: 6

Ismail
Ismail

Reputation: 109

FirebaseAuth.getInstance().signOut() before the intent.

Upvotes: 1

Lenos
Lenos

Reputation: 11

Try getting the context from the fragment instead

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
  super.onViewCreated(view, savedInstanceState)

  LogOut_btn.setOnClickListener {
    //FirebaseAuth.getInstance().signOut()
     val intent = Intent (view.context, Main::class.java)
    startActivity(intent)
  }
}

Upvotes: 1

Emma Borkent
Emma Borkent

Reputation: 71

Put this

LogOut_btn.setOnClickListener {
        //FirebaseAuth.getInstance().signOut()
         val intent = Intent (this, Main::class.java)
        startActivity(intent)



    }

inside the onCreate method of the Activity where the fragment is loaded

Upvotes: 1

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
    }
}

Upvotes: 1

Pedro Massango
Pedro Massango

Reputation: 5015

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 ([email protected], Main::class.java)
startActivity(intent)

Look at this sample bellow:

class MyFragment: Fragment(){

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val intent = Intent ([email protected], YOUR_NEXT_ACTIVITY_CLASS::class.java)
        startActivity(intent)
    }
}

Upvotes: 2

dasfima
dasfima

Reputation: 5301

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)
}

Upvotes: 51

Blue Jones
Blue Jones

Reputation: 385

I believe it would be something like

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

You must use the calling activities context

Upvotes: 2

Related Questions