[Kryonet] Handling the 'connecting' time - progress dialog or something

Hello!

I have an app with a menu with 4 options. I need to make that when you click a button, it checks if Kryo is connected and if not - pop a ProgressDialog and try to reconnect.

I don’t have any idea how this should be done. Tried with AsyncTask (I know how Async works, don’t worry), but I don’t know how it should be handled with my classes.

I have this menu class (it’s the main class in the app) where I call:

  makeConnection = new MakeConnection(this);
        makeConnection.start();


MakeConnection
is a class that connects to the server:

public class MakeConnection extends Thread {
    public Client client;
    public Context context;


    public MakeConnection(Context ctx){
        this.context = ctx;

    }

    @Override
    public void run() {
        client = new Client(9000000,9000000);
        new Thread(client).start();
        
        Network.register(client);

        client.addListener(new Listener.ThreadedListener(new Listener() {
            public void connected (Connection connection) {
            }

            public void received (Connection connection, Object object) {
       
           /*     if (object instanceof Network.PlayerPosition) {
                    Network.PlayerPosition pp = ((PlayerPosition)object);
                    gameLoop.player_opponent.x = pp.x;
                    gameLoop.player_opponent.y = pp.y;
               
                }*/


            }

            public void disconnected (Connection connection) {
                System.exit(0);
            }
        }));

        String host = "192.168.1.2";
        try {
            client.connect(5000, host, Network.port, 8889);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

I just don’t know how to handle moments like ‘connecting’ etc on a main thread - in the menu activity. I need to create a progressdialog, block it and update, when clients gets connected or reconnected again.

If there is anyone that could help, I would be grateful :slight_smile:

I would think about re-thinking your system here. Does it make sense that the main menu handles events like connections to a server? I would think that upon server connection, send an event to your menu to display whatever dialog you need. I think it would be to your benefit to read about the MVC design pattern, you could take some components of it and implement them into your game so there is a clear distinction between logic and, the data and your rendering.

Sorry this didn’t really answer your question, I just wanted to make a suggestion! You may have an easier time if you try to separate your logic here.