Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android-Libgdx, Calling Another Activity after the Game starts on Button click

I faced a major problem when I need to call another activity when the button is clicked after the Game is started. The Game is called via initiate(game, ) method from AndroidApplication interface.

In normal Activity, I can easily call the another Activity but it seems to be difficult to call another Activity from Libgdx class that implements AndroidApplication.

Could anyone suggest a proper method to call the Activity from Libgdx class that implements AndroidApplication interface?

I tried to do this for a week but it seems that my method is totally wrong..

Thanks in advance.

like image 973
denden130 Avatar asked Sep 26 '14 23:09

denden130


1 Answers

Define a callback interface in you LibGdx class, and use it to notify your AndroidLauncher to start the new activity.

For example in your LibGdx game class:

// Your Game class in the core package
public class MyGame extends Game {

    // Define an interface for your various callbacks to the android launcher
    public interface MyGameCallback {
        public void onStartActivityA();
        public void onStartActivityB();
        public void onStartSomeActivity(int someParameter, String someOtherParameter);
    }

    // Local variable to hold the callback implementation
    private MyGameCallback myGameCallback;

    // ** Additional **
    // Setter for the callback
    public void setMyGameCallback(MyGameCallback callback) {
        myGameCallback = callback;
    }

    @Override
    public void create () {
        ...
    }

    ...

    private void someMethod() {
        ...
        // check the calling class has actually implemented MyGameCallback
        if (myGameCallback != null) {

            // initiate which ever callback method you need.
            if (someCondition) {
                myGameCallback.onStartActivityA();
            } else if (someOtherCondition) {
                myGameCallback.onStartActivityB();
            } else {
                myGameCallback.onStartSomeActivity(someInteger, someString);
            }

        } else {
            Log.e("MyGame", "To use this class you must implement MyGameCallback!")
        }
    }
}

Then ensure your AndroidLauncher implements the required interface:

// Your AndroidLauncher
public class AndroidLauncher extends AndroidApplication implements MyGame.MyGameCallback {

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();

        // create an instance of MyGame, and set the callback
        MyGame myGame = new MyGame;
        // Since AndroidLauncher implements MyGame.MyGameCallback, we can just pass 'this' to the callback setter.
        myGame.setMyGameCallback(this);

        initialize(myGame, config);
    }

    @Override
    public void onStartActivityA() {
        Intent intent = new Intent(this, ActivityA.class);
        startActivity(intent);
    }

    @Override
    public void onStartActivityB(){
        Intent intent = new Intent(this, ActivityB.class);
        startActivity(intent);
    }

    @Override
    public void onStartSomeActivity(int someParameter, String someOtherParameter){
        Intent intent = new Intent(this, ActivityA.class);

        // do whatever you want with the supplied parameters.
        if (someParameter == 42) {
            intent.putExtra(MY_EXTRA, someOtherParameter);
        }
        startActivity(intent);
    }

}
like image 144
Sound Conception Avatar answered Oct 21 '22 00:10

Sound Conception