Invisible Node reappears after reattaching a few times o_O

I’ve been having problems with my App recently. I have a main Node which contains my model. It is located inside my main application class so it persists between the different activities.

[java]public static final Node mainNode = new Node(“Car”);[/java]

Then I have a class that handles loading a new model and attaching it to the mainNode.

This is used with the .enqueue() function

[java] public static class changeLineTask extends AsyncTask<String, Integer, Long> {



private ProgressDialog dialog;

private final AndroidHarness activity;



public changeLineTask(AndroidHarness activity) {

this.activity = activity;

dialog = new ProgressDialog(this.activity);

dialog.setCancelable(false);

}



@Override

protected Long doInBackground(final String… strings) {

Log.e(“LineLoader”, "Loading model: " + strings[0]);

Future<Object> task = activity.getJmeApplication().enqueue(new Callable<Object>() {

@Override

public Object call() throws Exception {



// Save the current cars color

Material oldColor = null;

if (carNode.getChild(“back-geom-0”) != null) {

oldColor = ((Geometry) carNode.getChild(“back-geom-0”)).getMaterial();

}



while (carNode.getChildren().size() > 0) {

carNode.removeFromParent();

carNode.detachAllChildren();

((DesktopAssetManager) activity.getJmeApplication().getAssetManager()).clearCache();

System.gc();

}



long t0, t1;

t0 = System.currentTimeMillis();

do {

t1 = System.currentTimeMillis();

}

while (t1 - t0 < 1000);



for (String s : new String[]{“model”, “top”, “sides”, “front”, “back”, “doors”, “interior”}) {

try {

Log.e(“ModelLoader”, “assets/models/lines/” + strings[0] + “/” + s + “.j3o”);

System.gc();

carNode.attachChild(activity.getJmeApplication().getAssetManager().loadModel(“assets/models/lines/” + strings[0] + “/” + s + “.j3o”));

} catch (OutOfMemoryError e) {

}

}



for (String s : new String[]{“windowglass”, “clearglass”, “redglass”}) {

try {

Log.e(“ModelLoader”, “assets/models/lines/” + strings[0] + “/” + s + “.j3o”);

System.gc();

Node glassNode = (Node) activity.getJmeApplication().getAssetManager().loadModel(“assets/models/lines/” + strings[0] + “/” + s + “.j3o”);

glassNode.getChild(0).setMaterial(activity.getJmeApplication().getAssetManager().loadMaterial(“assets/materials/” + s + “.j3m”));

glassNode.getChild(0).setQueueBucket(RenderQueue.Bucket.Transparent);

carNode.attachChild(glassNode);

glassNode = null;

} catch (OutOfMemoryError e) {

}

}



for (String s : new String[]{“windowglass_left”, “windowglass_right”}) {

try {

Log.e(“ModelLoader”, “assets/models/lines/” + strings[0] + “/” + s + “.j3o”);

System.gc();

Geometry glassNode = (Geometry) activity.getJmeApplication().getAssetManager().loadModel(“assets/models/lines/” + strings[0] + “/” + s + “.j3o”);

glassNode.setMaterial(activity.getJmeApplication().getAssetManager().loadMaterial(“assets/materials/” + s + “.j3m”));

glassNode.setQueueBucket(RenderQueue.Bucket.Transparent);

carNode.attachChild(glassNode);

glassNode = null;

} catch (OutOfMemoryError e) {

}

}



for (Spatial s : carNode.getChildren()) {

s.setLocalRotation(new Quaternion().fromAngles(-1.57079633f, 0, 0));

s.scale(0.033f);

}



// Set the cars color to the old color

for (String s : new String[]{“top”, “sides”, “front”, “back”, “doors”}) {

if (carNode.getChild(s + “-geom-0”) != null && oldColor != null) {

carNode.getChild(s + “-geom-0”).setMaterial(oldColor);

} else {

changeColor(mCurrentColor);

}

}



// Attach the car to the carnode and return

mainNode.attachChild(carNode);

return null;

}

});

while (!task.isDone()) {

// Wait for model to finish loading

}

return null;

}



@Override

protected void onPreExecute() {

dialog.setMessage(“Loading…”);

dialog.show();

}



@Override

protected void onPostExecute(Long result) {

dialog.cancel();

dialog = null;

}

}

[/java]

Now, my problem is that when I switch activity the model is invisible. But if I change it exactly two times, it reappears.

Changing one time won’t do any good, but changing it exactly twice does, and this is whats confusing me…



I did some debugging, and can see that the meshes are attached to the nodes and such, and getting the vertex count on the mainNode matches a fully loaded model. But for some odd reason it stays invisible.

Oh yeah, almost forgot. I got two activities which use the Node.

From my main menu, if I go into activity two, car loads, shows just fine. Then going back and into activity one, the car is invisible.

If I start by going into activity one, and then go back to the menu, and into activity two, the model shows just fine.

Maybe you add the light only in one of the states?

@normen said:
Maybe you add the light only in one of the states?

The light is added to the rootNode in the individual activities, and it wouldn't explain the need to load twice :/
I presume it might have something to do with not using the same assetManager for both activities, Ill try some stuff and get back

This stuff is pretty straightforward and should not show any unpredictable behavior like this. Its most likely in your code.

I think its due to one of the activities not freeing the objects. I noticed in the log that the following line doesn’t get called when the problematic activity closes.

[java]03-29 14:25:48.745: I/OGLESContext(10452): Display destroyed.[/java]



Working activity shutdown:

[java]

03-29 14:25:48.745: I/OGLESContext(10452): Display destroyed.

03-29 14:25:48.777: I/AndroidHarness(10452): onPause

03-29 14:25:48.910: D/OpenGLRenderer(10452): Flushing caches (mode 0)

03-29 14:25:49.128: I/AndroidHarness(10452): onStop

03-29 14:25:49.128: I/AndroidHarness(10452): onDestroy

[/java]



Non-working activity shutdown:

[java]

03-29 14:25:28.792: I/AndroidHarness(10452): onPause

03-29 14:25:28.933: D/OpenGLRenderer(10452): Flushing caches (mode 0)

03-29 14:25:29.152: I/AndroidHarness(10452): onStop

03-29 14:25:29.191: I/AndroidHarness(10452): onDestroy

[/java]

Okay, added some logging to JME OGLESContext

[java]protected void deinitInThread() {

Log.e(“OGLESContext”, “deinitInThread() !!!”);[/java]



In one of the activities this doesn’t get called…



EDIT: I have checked all the AndroidHarness methods I override, commented all of them out one by one, no difference.

Seems like this is a general problem when calling finish() in AndroidHarness…

I’ve replace my

[java]finish()[/java]

with

[java]finishOnAppStop = true;

app.stop(true);[/java]

And it’s fixed a few of these occurences of that log message not showing up

Why do you want to use activities? They do not translate to other platforms, just do your separate screens with nifty…?