0

When I try using startActivity inside onCreate() function inside any Activity class, It works without any problem. But when I need to use it inside any function that I call indirectly inside Activity class, Its color will be red and I get this error:

Unresolved reference: startActivity

val intent = Intent(instance.applicationContext, MainActivity::class.java)
startActivity(intent)

Note: I am using instance as an imported argument in Global class constructor.

Could anyone help please?

weera
  • 884
  • 1
  • 10
  • 21
  • Can you show the whole function that includes this code? – Tenfour04 Jul 19 '20 at 21:10
  • I already found the solution : https://stackoverflow.com/questions/11222882/start-intent-without-oncreate Thank you! – weera Jul 19 '20 at 22:12
  • Note that in the question you've linked the call to `startActivity()` is happening inside `AdapterView.OnItemClickListener` and not inside `MainActivity`. If this is the case with your code as well you should've mentioned it. `startActivity` is a method declared in the `Activity` class, so you need `this` to point to an instance of `Activity` for the call to work. Your question just doesn't have enough detail to make it answerable, please pay attention to this in the future. – Egor Jul 19 '20 at 22:30

0 Answers0