Reputation: 8325
I have an application that uses JNI and the NDK to run native C++ in background of the UI. The native layer is initialized in onCreate
, and finalized in onDestroy
.
public class XXXXActivity extends Activity
{
TermScreenView mTermScreenView = null;
@Override
protected void onCreate( Bundle savedInstanceState )
{
super.onCreate( savedInstanceState );
try
{
this.setRequestedOrientation( ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE );
setContentView( R.layout.activity_XXXX );
mTermScreenView = (TermScreenView) findViewById( R.id.termScreenView );
if( ! initializeNative() )
{
Toast.makeText( getBaseContext(), "Failed to initialize", Toast.LENGTH_LONG ).show();
throw new Exception( "Failed to initialize native layer" );
}
}
catch( Exception exc )
{
exc.printStackTrace();
}
}
@Override
public void onDestroy()
{
finalizeNative();
super.onDestroy();
}
public native boolean initializeNative();
public native boolean finalizeNative();
// Etc
}
For some reason, after initializeNative
ends successfully by returning true
and without throwing any exception in the native code and onCreate
ends successfully too, onDestroyed
gets called several milliseconds after.
Nothing is supposed to be called once onCreate ends in my application, except onDestroy
when the user quits it.
I don't know why onDestroy
is called since onCreate
succeeds.
What could be the reason ?
Thank you :)
EDIT : Here is the code from initializeNative :
jboolean XXXXActivity_initializeNative( JNIEnv * pEnv, jobject pActivityObject )
{
TRACE_I( g_pJniLog, "::XXXXActivity_initializeNative() - Begin." );
if( NULL != g_pAppAdapt )
{
TRACE_I( g_pJniLog, "::XXXXActivity_initializeNative() - End, already called." );
return false;
}
jclass pActivityClass = pEnv->FindClass( "XXXXActivity" );
assert( NULL != pActivityClass );
TRACE_I( g_pJniLog, "::XXXXActivity_initializeNative() - pActivityClass = %p.", pActivityClass );
g_pActivityClass = (jclass) pEnv->NewGlobalRef( pActivityClass );
assert( NULL != g_pActivityClass );
g_pActivityObject = (jobject) pEnv->NewGlobalRef( pActivityObject );
assert( NULL != g_pActivityObject );
TRACE_I( g_pJniLog, "::XXXXActivity_initializeNative() - Creating XXXXActivityAdapter." );
g_pAppAdapt = new XXXXActivityAdapter();
TRACE_I( g_pJniLog, "::XXXXActivity_initializeNative() - Initializing XXXXActivityAdapter." );
g_pAppAdapt->InitializeInstance();
TRACE_I( g_pJniLog, "::XXXXActivity_initializeNative() - End, success." );
return true;
}
I don't think it would be relevant to provide more since since it would get very big. I can tell you that the function doesn't throw a native exception and doesn't return false.
Upvotes: 0
Views: 360
Reputation: 8325
I found the problem : setting the screen orientation is destroying the activity and recreating it.
To fix the problem i can either not change the screen orientation or check whether the Bundle passed to onCreate is null.
Thanks for those who helped me.
Upvotes: 1