king
king

Reputation: 507

Load an activity from libgdx project

I'm trying to use some Android widget, like the scrolling listbox for listing the scores of players. I have my basic Libgdx project so there are no class hineriting from Activity. I'm trying to incorporate a class extending Activity. The problem(and also an advantage) is that libgdx is not specific to Android. So I have to create an Interface to handle the Android part Desktop part iOS part etc... like described on this page: http://code.google.com/p/libgdx/wiki/ApplicationPlatformSpecific I did it and it's "working", what I mean by workin it's that the program go in the right class hineriting from my Interface. This Android class extends Activity. I have implemented the onCreate() function but it's not working, I put some breakpoint in this onCreate() methode but there are ignore, so I guess there are not going in the function. I read that I have to call:

Intent myIntent = new Intent(this, AvitivityName.class);
startActivity(myIntent);

I'm not doing this two lignes above. Should I put them? If so, where?

Edit

OK, first thank you for your very detail answer. I understand the first par of the code (the class AndroidInterface). But I'm not sure to understand the second part. This is the default code in the android part when you build a libgdx project:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();

    initialize(new MyMain(), cfg);
}

And this is what I add in this function:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
    cfg.useGL20 = true;

    IRank rank = new ARank();
    initialize(new MyMain(rank), cfg);
}

So when it create the MyMain() object it will pass the Interface. So like that I can call the appropriate Interface for each support. But when the core of the project is going to call my interface I don't know how to go back to the main activity, the "MainActivity extends AndroidApplication" one. I don't know how it exactly works. For exemple if i do like you said and pass "this" to my ARank() object (ARank(this)). And then when I'll want to go back in themain libgdx activity, I don't know how to do it. I'm a begginer so I dont know how works

Intent i = new Intent(getApplicationContext(), TestActivity.class);
            startActivity(i);

I know that the variable "i" should represent this class:

class MainActivity extends AndroidApplication

But if I call statrtActivity(the main activity); I don't want this function recall the onCreate method.

I'm sorry for my message, it's problably not clear, but i'm a little desperate.

Thank you if you can answer back.

Upvotes: 0

Views: 1175

Answers (1)

cavpollo
cavpollo

Reputation: 4318

If im correct what you are trying to do is to open another activity from LibGDX.

Ive done that before by doing what you just described. I created an interface that does different things on each project.

Now, let me explain the not so elegant solution that I found a while back in a hurry.

In your android interface class you need to have a reference to your main class. Also, you would need a function to switch to your other activity by talking to your MainActivity.

public class AndroidInterface implements MyInterface{
    private MainActivity _mainActivity;
    ...
    public AndroidInterface (MainActivity activity){
        _mainActivity = activity;
    }
    ...
    public void switchActivity(int activityID) {
        _mainActivity.startMyActivity(activityID);
    }
}

Pretty simple, isnt it?

Now, lets go to your MainActivity class. You would initialize and do the other stuff you would put on the onCreate method, but dont forget to create an AndroidInterface with your MainActivity as a parameter. So, lets say your other activity is called TestActivity and it will be called if your activityID is 10.

public class MainActivity extends AndroidApplication{
    ...
    public MainActivity (){
        ...
    }
    ...
    public void onCreate(Bundle savedInstanceState) {
        ...
        AndroidInterface androidFunctions = new AndroidInterface((MainActivity)this);
        ...
    }
    ...
    public void startMyActivity(int activityID) {
        switch(activityID){
            case 10:
                Intent i = new Intent(getApplicationContext(), TestActivity.class);
                startActivity(i);
                break;
        }
    }
}

So in the end it would end up looking more or less like that. You would call your AndroidInterface and it would start another activity by calling your Main Activity and getting its context there.

So you can see I do all that work because I need the applicationContext of the Main Activity in order to start another Activity.

There must be a more elegant and correct way to do it. Im pretty sure that keeping a reference to the MainActivity in the AndroidInterface isnt a good idea, but this got me out of trouble fast if thats what you are looking for. You can work from that.

Either way I hope this helps. =)

Upvotes: 1

Related Questions