1

I try to open an Kotlin Activity from a LibGDX class but I am stuck ...

I already read How to start a Activity of Kotlin from Java android and type something like the example : startActivity(new Intent(context, myKotlinActivity.kt))

The purpose of my code is to redirect the user from a menu to an Kotlin Activity.

 @Override
 public void handleInput() {
     if(Gdx.input.justTouched()) {
          dispose();
          startActivity(new Intent(context, MyActivity.kt))
     }
 }

My error is : Cannot resolve method startActivity(Intent)

What am I forgetting ?

Thank you very much and I apologize if it's a dumb question ...

Santanu Sur
  • 10,997
  • 7
  • 33
  • 52
  • That's not how you open activities in Kotlin - you should pass the Java class of your `MyActivity` class instead of using your class' file name: (in this case it should be `MyActivity::class.java` instead of `MyActivity.kt`) – Edric Oct 22 '19 at 11:14
  • LibGDX has separate modules in your project for "core" code and "android" code. This is so the core code can be used in multiple platforms, like desktop JVM or iOS. For this reason, core code cannot call anything that is platform specific, like Activity classes. You need to create an interface that your `android` module can implement with the code to open the activity. – Tenfour04 Oct 22 '19 at 13:02
  • https://github.com/libgdx/libgdx/wiki/Interfacing-with-platform-specific-code – Tenfour04 Oct 22 '19 at 13:03
  • 1
    Possible duplicate of [Android Intent and startActivity in Libgdx (non Activity or AndroidApplication class)](https://stackoverflow.com/questions/12693992/android-intent-and-startactivity-in-libgdx-non-activity-or-androidapplication-c) – Tenfour04 Oct 22 '19 at 13:07
  • Are you wanting to close the game as you open another activity? You shouldn't be calling `dispose()` directly on your Screen unless switching literal LibGDX screens. If you're just opening another Activity, the libGDX Activity will still be alive on the task stack. If you want to close the game, you should call `finish()` on its host Activity (which must also be done with interfaces since it is platform-specific code). – Tenfour04 Oct 22 '19 at 13:17
  • I am not totally fluent in English so it is a bit difficult to me to explain, I am sorry. I have a menu created width libgdx (I have 2 buttons). And (for now,) when I touch the screen, I want open a new activity made with Kotlin (Camera for AR). So, I don't really know what it's should be in the logic of the process ... – Tenecifer06 Oct 22 '19 at 13:24
  • But do you want the user to be able to press back in the camera activity and get back into your game? – Tenfour04 Oct 22 '19 at 16:07

2 Answers2

0

Starting activity from classes which are not context based can be done with setting context to this class, but you have to be careful, because it can cause memory leak. On the other hand you can use application context. With creating your Application class for your app. Check here how you can achieve it: Application singleton use in Android

After you have instance created, you can start your activity from any class you wanted like:

MyApplication.getInstance().startActivity(MyApplication.getInstance, MainAcitivity::class);
mmmatey
  • 666
  • 8
  • 15
  • 2
    You should not use ApplicationContext for navigating between Activities, because it requires the `FLAG_ACTIVITY_NEW_TASK`, and you probably don't want to start a new task for each Activity. – EpicPandaForce Oct 22 '19 at 11:53
  • @EpicPandaForce exactly, I forgot to mention that developer should also set FLAG_ACTIVITY_NEW_TASK in that case.. my answer is useful only when there is no other option to pass or request context in specific class. – mmmatey Oct 22 '19 at 11:59
  • The OP doesn't have access to Application from within this module either because they are using LibGDX. – Tenfour04 Oct 22 '19 at 13:10
0

I think I find my answer here : Android-Libgdx, Calling Another Activity after the Game starts on Button click

I will check on my tablet...