The input queue runs concurrent with the native activity lifecycle, and so the getKey helper Java method might be called after the app has been destroyed. This is particularly likely for "back" key presses that destroys activities. Change the getKey method to be static so that it can be called outside the app lifecycle. Run `go generate ./cmd/gomobile` to update the compiled dex file that contains GoNativeActivity. Fixes golang/go#27652 Change-Id: Id2c863ee07e5447f033e67d6948fbfe746916ffa Reviewed-on: https://go-review.googlesource.com/135215 Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
68 lines
1.8 KiB
Java
68 lines
1.8 KiB
Java
package org.golang.app;
|
|
|
|
import android.app.Activity;
|
|
import android.app.NativeActivity;
|
|
import android.content.Context;
|
|
import android.content.pm.ActivityInfo;
|
|
import android.content.pm.PackageManager;
|
|
import android.os.Bundle;
|
|
import android.util.Log;
|
|
import android.view.KeyCharacterMap;
|
|
|
|
public class GoNativeActivity extends NativeActivity {
|
|
private static GoNativeActivity goNativeActivity;
|
|
|
|
public GoNativeActivity() {
|
|
super();
|
|
goNativeActivity = this;
|
|
}
|
|
|
|
String getTmpdir() {
|
|
return getCacheDir().getAbsolutePath();
|
|
}
|
|
|
|
static int getRune(int deviceId, int keyCode, int metaState) {
|
|
try {
|
|
int rune = KeyCharacterMap.load(deviceId).get(keyCode, metaState);
|
|
if (rune == 0) {
|
|
return -1;
|
|
}
|
|
return rune;
|
|
} catch (KeyCharacterMap.UnavailableException e) {
|
|
return -1;
|
|
} catch (Exception e) {
|
|
Log.e("Go", "exception reading KeyCharacterMap", e);
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
private void load() {
|
|
// Interestingly, NativeActivity uses a different method
|
|
// to find native code to execute, avoiding
|
|
// System.loadLibrary. The result is Java methods
|
|
// implemented in C with JNIEXPORT (and JNI_OnLoad) are not
|
|
// available unless an explicit call to System.loadLibrary
|
|
// is done. So we do it here, borrowing the name of the
|
|
// library from the same AndroidManifest.xml metadata used
|
|
// by NativeActivity.
|
|
try {
|
|
ActivityInfo ai = getPackageManager().getActivityInfo(
|
|
getIntent().getComponent(), PackageManager.GET_META_DATA);
|
|
if (ai.metaData == null) {
|
|
Log.e("Go", "loadLibrary: no manifest metadata found");
|
|
return;
|
|
}
|
|
String libName = ai.metaData.getString("android.app.lib_name");
|
|
System.loadLibrary(libName);
|
|
} catch (Exception e) {
|
|
Log.e("Go", "loadLibrary failed", e);
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void onCreate(Bundle savedInstanceState) {
|
|
load();
|
|
super.onCreate(savedInstanceState);
|
|
}
|
|
}
|