We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 1815 - SDL_AndroidGetActivity() does not work.
Summary: SDL_AndroidGetActivity() does not work.
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: main (show other bugs)
Version: HG 2.0
Hardware: All Android (All)
: P2 major
Assignee: Gabriel Jacobo
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-04-20 02:19 UTC by Pallav Nawani
Modified: 2013-04-22 15:53 UTC (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Pallav Nawani 2013-04-20 02:19:55 UTC
The function returns a local jobject, which goes out of scope as soon as function returns, thanks to LocalReferenceHolder refs(__FUNCTION__);

This code works:

AAssetManager *IcOSAndroidGetAssetManager()
{

	SDL_Log("%s(): Attempting to obtain JNI pointer", __FUNCTION__);
	JNIEnv *aEnv = (JNIEnv *)SDL_AndroidGetJNIEnv();
	assert(aEnv);

	SDL_Log("%s(): Got JNI", __FUNCTION__);

	jclass aSdlActivityClass = aEnv->FindClass("org/libsdl/app/SDLActivity");
	SDL_Log("%s(): Got SDL Class (%x)", __FUNCTION__, aSdlActivityClass);

    jmethodID aStaticMid = aEnv->GetStaticMethodID(aSdlActivityClass, "getContext", "()Landroid/content/Context;");
    jobject aContext =  aEnv->CallStaticObjectMethod(aSdlActivityClass, aStaticMid);

	//jobject aContext = (jobject)SDL_AndroidGetActivity();
	SDL_Log("%s(): Got Activity (%x)", __FUNCTION__, aContext);

	jclass aSdlClass = aEnv->FindClass("android/content/Context");
	SDL_Log("%s(): Got Class (%x)", __FUNCTION__, aSdlClass);

	jmethodID aJavaMethodID = aEnv->GetMethodID(aSdlClass, "getAssets", "()Landroid/content/res/AssetManager;");
	SDL_Log("%s(): Got Method ID (%x)", __FUNCTION__, aJavaMethodID);
    jobject aJavaAssetManager = aEnv->CallObjectMethod(aContext, aJavaMethodID);
	SDL_Log("%s(): Got Asset Manager (%x)", __FUNCTION__, aJavaAssetManager);

	return AAssetManager_fromJava(aEnv, aJavaAssetManager);
}

Uncomment the commented line to make it fail.
Comment 1 Gabriel Jacobo 2013-04-20 11:20:12 UTC
Good find!
Comment 2 Gabriel Jacobo 2013-04-22 10:21:16 UTC
Fixed in http://hg.libsdl.org/SDL/rev/0cb47cc139d3

You should remember to delete the local reference manually!
Comment 3 Philipp Wiesemann 2013-04-22 15:53:31 UTC
(In reply to comment #2)
> Fixed in http://hg.libsdl.org/SDL/rev/0cb47cc139d3
> 
> You should remember to delete the local reference manually!

Maybe it would be useful to add this warning in the SDL_system.h file because SDL_AndroidGetActivity() is meant for library users and most of them will not look into the implementation itself (the function is not used internally).