Warm tip: This article is reproduced from stackoverflow.com, please click
android kotlin

How to make an Intent activity from Fragment? (Error on Activity name)

发布于 2020-03-31 22:54:45

I'm trying to make an intent from fragment but getting an error. I see there are a lot of posts on this subject, but nothing resolves my problem. I already added context and getActivity().

The error I am getting is on RegistrationActivity (Unresolved reference). How can I reference it? (android studio hints importing RegistrationActivity to ProfileFragment, but I would say that is not a good way to do it). Here is the code:

val intent = Intent(this@ProfileFragment.context, RegistrationActivity::class.java).apply {}
getActivity().startActivity(intent)

Thanks.

Questioner
ljerka
Viewed
55
ljerka 2020-01-31 20:18

After your comments I imported RegistrationActivity and now it works. It is good to remember that Fragments don't behave like Activities - in Activity you don't have to import another Activity, and in Fragments you need to do it.