Problem with progressdialog

Started by nIkO_mx, July 04, 2015, 03:01:27 PM

Previous topic - Next topic

nIkO_mx

Hello, congratulations for this useful forum!
I'm creating an app based on JPCT demo to load and view obj files.
I have implemented funcions for load, pinch, drag and rotate objects.
As described in some other topic in this forum the Loader takes some time to load OBJ's, so I'm trying to insert a progressdialog to inform the user that loading is in progress.
I'm using an Object3D that i clear and reload as I need.

This is the code I've written inside the menu:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.Ruota:
                iF = 0;
                break;
            case R.id.Disegna:
                iF = 1;
                break;
            case R.id.Cancella:
                iF = 2;
                break;
            case R.id.Load:
                ProgressDialog PrgD = new ProgressDialog(MainActivity.this);
                PrgD.setTitle("mymessage1");
                PrgD.setMessage("mymessage2");
                PrgD.setCancelable(false);
                PrgD.setIndeterminate(true);
                PrgD.show();
                Tm = true;

                try {
                    world.removeObject(mesh);
                    mesh = null;
                }catch (Exception e){
                    throw new RuntimeException(e.toString());
                }
                model = getResources().openRawResource(R.raw.model);
                mtl = getResources().openRawResource(R.raw.material);
                mesh = Object3D.mergeAll(Loader.loadOBJ(model, mtl, 1));
                renderer.ObjLoad();
                PrgD.dismiss();
                break;


renderer.ObjLoad() is a void inside MyRenderer class where i set cameras, textures etc for the new model loaded.
Everithing works fine, i can read models from raw dir and display them with texture, but I can't see the progressdialog!
The dialog works fine untill i call Loader function inside my code (if i comment out the "mesh =..." line it works!).
If i put back the loader line the loader works, but instead of progressdialog i see empty screen..
I've also tried with an AsyncTask, calling the loader in Background, but the problem still remains: i'm not able to display the progress dialog...
I'm a beginner with coding, so maybe i'm doing some dumb error....can someone help me?
Thanks!

EgonOlsen

When running an OpenGL based app, there are bascially two threads doing things. One is the renderer thread, in which the drawing happens. Whatever you do directly in indirectly in onDrawFrame will block this thread and will make it impossible to render anything via OpenGL until it's unblocked again. The same is true for the UI thread, which is also the thread that does the event handling for touch events, button clicks etc. If this is blocked, no Android UI elements will be drawn and no input events recognized.
In your case, I'm not sure where the actual loading happens. But in any case, try to put the loading in another thread (try the Thread class, if AsyncTask didn't help...albeit I would expect it work fine to solve your problem).

nIkO_mx

hello! Thank you so much for the quick reply!
I've tried also with thread but i still get black screen (background color) during loading... here is my code:  do you have some more advice for me??
@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        switch (id) {
            case R.id.Ruota:
                iF = 0;
                break;
            case R.id.Disegna:
                iF = 1;
                break;
            case R.id.Cancella:
                iF = 2;
                break;
            case R.id.Load:
                Tm = true;
                try {
                    world.removeObject(mesh);
                    mesh = null;
                } catch (Exception e) {
                    throw new RuntimeException(e.toString());
                }
                model = getResources().openRawResource(R.raw.model);
                mtl = getResources().openRawResource(R.raw.material);
                PrgD = ProgressDialog.show(MainActivity.this, "Please wait ...",  "Task in progress ...", true);
                PrgD.setCancelable(true);
                Thread tt = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            mesh = Object3D.mergeAll(Loader.loadOBJ(model, mtl, 1));
                            renderer.ObjLoad();
                            Logger.log("MESH LOADED");
                        } catch (Exception e) {

                        }
                        PrgD.dismiss();
                    }
                });
                tt.run();
                break;
        }
        return false;
    }


thank you!

n.

EgonOlsen

Don't call tt.run(), because that's just a synchronous method call of the run()-method. Use tt.start(); instead.

nIkO_mx

Hello Egon!
Your tip was really useful!
I've tried to run an AsyncTask using .start() method and it finally got working!!
So, if someone have the same problem: yes AsyncTask is the answer!

Thank you so much!

n.