Re: OFActivity is not working - SOLVED!

Hi everyone,

I am developing an APP for android and i have been having problems when trying to configure the OFActivity.

Any change in the OFActivity.java is taking effect in the final APP. I mean, the APP works fine, but as soon as the mobile is locked, I think the data (Bundle savedInstanceStated) doesn’t save neither loads. The consequence is that once unlocked, the APP crashes and loads again (instead of aproppietaly calling resume() ). So it calls onCreate again.

I tried to override onDestroy (not implemented) to destroy the APP in a right way but it didnt work either.
Testing other OFexamples, I realized that it happens to those examples too.

Native objects created in the layout in java aren’t working either (their listeners are writen in the OFActivity.java file, so…)

For me, it looks like the OFActivity.java is not working at all (I tried the buttons, writing extra code,etc…). Although, this is kind of impossible because the application launches and works fine (but doesn’t resume after locking).

Has anyone any explanation or solution? Has anyone managed to compile an app with functionality in the OFActivity?

For more information I am compiling with eclipse and working in linux.

thks in advanced dudes!

Yes, I’ve written apps and run the examples and they work fine on my Android devices (Nexus One and Galaxy Tab 10.1).

Can you post your OFActivity.java?

in fact, the examples are working on my device too, but they sometimes crash after locking.

Anyway, the activity is this one:

  
package cc.openframeworks.diceRoller3D;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.KeyEvent;  
import android.view.Menu;  
import android.view.MenuItem;  
import cc.openframeworks.OFAndroid;  
  
  
public class OFActivity extends Activity{  
	  
  
  
	@Override  
    public void onCreate(Bundle savedInstanceState)  
    {   
        super.onCreate(savedInstanceState);  
        String packageName = getPackageName();  
        setContentView(R.layout.main);  
  
        ofApp = new OFAndroid(packageName,this);  
          
    }  
	  
	@Override  
	public void onDetachedFromWindow() {  
	}  
	  
    @Override  
    protected void onPause() {  
    	  
        super.onPause();  
          
        ofApp.pause();  
    }  
  
    @Override  
    protected void onResume() {  
        super.onResume();  
         
        ofApp.resume();  
          
    }  
      
    @Override  
    public boolean onKeyDown(int keyCode, KeyEvent event) {  
        OFAndroid.onKeyDown(keyCode);  
        return super.onKeyDown(keyCode, event);  
    }  
      
    @Override  
    public boolean onKeyUp(int keyCode, KeyEvent event) {  
        if ((keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)) {  
            if( OFAndroid.onBackPressed() ) return true;  
            else return super.onKeyUp(keyCode, event);  
        }  
          
        OFAndroid.onKeyUp(keyCode);  
        return super.onKeyUp(keyCode, event);  
    }  
  
  
	OFAndroid ofApp;  
      
	  
	  
    // Menus  
    // [http://developer.android.com/guide/topics/ui/menus.html](http://developer.android.com/guide/topics/ui/menus.html)  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
    	// Create settings menu options from here, one by one or infalting an xml  
        return super.onCreateOptionsMenu(menu);  
    }  
      
    @Override  
    public boolean onOptionsItemSelected(MenuItem item) {  
    	// This passes the menu option string to OF  
    	// you can add additional behavior from java modifying this method  
    	// but keep the call to OFAndroid so OF is notified of menu events  
    	if(OFAndroid.menuItemSelected(item.getItemId())){  
    		  
    		return true;  
    	}  
    	return super.onOptionsItemSelected(item);  
    }  
      
  
    @Override  
    public boolean onPrepareOptionsMenu (Menu menu){  
    	// This method is called every time the menu is opened  
    	//  you can add or remove menu options from here  
    	return  super.onPrepareOptionsMenu(menu);  
    }  
	  
}  
  

thks!

Can you provide a repeatable way to crash one of the android examples on your device? Also, when you say ‘locking’, do you mean turning the device’s screen off and then on again, so that it goes to the lock screen?

ok, let’s see.

1.open the androidTouchExample (in this example you can see if the app resets, because it will not store data).
2.compile it.
3.once the app is running in your device (in my case, SAMSUNG GALAXY ACE), lock it and unlock it.

When I do this, the app doesn’t RESUME (the data of the position and color of the balls is not kept in the memory). Instead, the app RESTARTS with a new data (randomly defined as its coded, in this case).

Does this happen to you?

thks dude.

look, I did a little graphic with the info in the LOGCAT:

the surface ( the openGL context ) is destroyed every time an aplication is paused. also onCreate is called everytime an application starts, even from the paused state. there’s some things that i need to review in that state cycle but in principle both things are normal.

OF restores images, fonts and textures in the videograbber automatically when the application is resumed but every other openGL resource should be restored manually

ok, arturo. This makes sense as we have a lot of gl stuff happening.

any idea of how to face this?

I was thinking in something like this:

http://developer.android.com/resources/tutorials/opengl/opengl-es20.html

Well, we’ll take a look and post it as soon as we fix.

thks!

PD: for the record, the APP we’re developing is called dice Roller 3D (there’s a free version). it’s already uploaded to googleplay (even if it doesn’t run perfectly yet) if you want to take a look at it.

finding.jsc,

This guy was so miffed by this Android behavior that he even made a video tutorial about how he went about the context destroy problem.

http://www.youtube.com/watch?v=J4w9hXRrU5E

Apparently you have to recreate your render context every time destroy is called.

Good luck!

How about fonts? I have seen fonts getting strangely maimed when the app is restored. Do they have to be reloaded from storage again?

no, fonts are also automatically reloaded by OF.

Hi everyone!

i read that all the GL data can be saved thks to JNI functionality mentioned in this post:

http://forum.openframeworks.cc/t/how-do-you-forward-ui-values-from-viewsmenus-to-of/7148/0

that looks awesome and would eventually solve the problems I am having when trying to recreate de GL context.

The problem is that this is apparently not working. I tried to implement all kinds of JNI stuff in the OFActivity.java and IT’S NOT WORKING AT ALL. Even the LOGS are not showing what they should in the LOGCAT.

I have even tried to delete all OFActivity.java code but then the APP is still working! Then, the app is probably looking for the functions who knows where ( !!! )

In order to reach the problem, I tried compiling androidFontExample and simply adding a LOG in the OFActivity.java
The result screenshot is attached.

As you can see, no LOGCAT messages other than the standard.

As i could not figure out what was going on - i think OFActivity doesn’t work ( the override funcitons or whatever) but something must be working as the application runs-, I decided to change the native functions’ LOGCAT messages. But again, I got no changes at all.

well, I am starting to think that there’s something I am missing about OFActivity.

Any ideas?

thks in advanced!

mmh, that should work, try deleting everything in bin except for the data folder in case the makefile is not refreshing properly the java builds. also i fixed some particular cases of texture reloading not working in github develop some days ago

jeje…

that worked.

i deleted the entire BIN folder but the DATA folder and then the right activity was used.

finally, the activity is returning what is had to. Maybe, it would be cool to write this in the OF eclipse instalation tut. I dont know if this is a general issue or something especific.

damm that was a little think.

thks a lot!!

PD: i will keep working in the GL and post something when the 3d engine+data restores properly.

are you using the download from the webpage or the version from git? git develop has lots of fixes, also try changing the Makefile with the new one from any of the examples, that should fix this problem