Search code examples
androidandroid-activitylibgdxandroid-context

How to getContext() in LIBGDX android app


I need to get the Context of my app but in my main Class extends from Game so I can not extends from Activity. Does anybody know how to do it?

Thank you!


Solution

  • Interfacing is the way to go since you can't access Android specific code from Core module.

    Step 1: Create the interface (CORE MODULE)

    public interface MyInterface {
    
        void manipulateContext();
    
        void manipulateContextWithExtraParams(String example, int example2);
    }
    

    Step 2: Implement the interface (ANDROID MODULE)

    import android.content.Context;
    
    public class InterfaceImplementation implements MyInterface {
    
        private Context context;
    
        public InterfaceImplementation(Context context) {
            // Store the context for later use
            this.context = context;
        }
    
        @Override
        public void manipulateContext() {
            // Do something with the context, this is called on the core module
            System.out.println(context);
        }
    
        @Override
        public void manipulateContextWithExtraParams(String example, int example2) {
            if (example2 == 1) {
                System.out.println(example + context);
            } else {
                System.out.println(example);
            }
        }
    }
    

    Step 3: Send the implemented interface your game (ANDROID MODULE)

    import android.os.Bundle;
    
    import com.badlogic.gdx.backends.android.AndroidApplication;
    import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
    import com.frontanilla.helping.getcontext.InterfaceImplementation;
    import com.frontanilla.helping.getcontext.MyGame;
    
    public class AndroidLauncher extends AndroidApplication {
        @Override
        protected void onCreate (Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
    
            InterfaceImplementation interfaceImplementation = new InterfaceImplementation(this);
    
            // Here we send the implementation to our Game in Core module
            initialize(new MyGame(interfaceImplementation), config);
        }
    }
    

    Step 4: Store and use the methods you defined on your interface (CORE MODULE)

    import com.badlogic.gdx.Game;
    
    public class MyGame extends Game {
    
        private MyInterface myInterface;
    
        public MyGame(MyInterface myInterface) {
            // Store for later use
            this.myInterface = myInterface;
        }
    
        @Override
        public void create() {
            // Example of manipulating the Android Context indirectly from Core module
            myInterface.manipulateContext();
            myInterface.manipulateContextWithExtraParams("Hello", 2);
        }
    }
    

    As you can see, you will not be manipulating the Context from the core module directly, instead, place that logic on the InterfaceImplementation class