Skip to content

Commit a89aa46

Browse files
committed
Do not raise exception when NativeScriptApplication.onCreate is called for a second time during initialization.
1 parent 8661343 commit a89aa46

2 files changed

Lines changed: 80 additions & 62 deletions

File tree

src/jni/NativePlatform.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,12 @@ void NativePlatform::AppInitCallback(const v8::FunctionCallbackInfo<v8::Value>&
284284

285285
// TODO: find another way to get "com/tns/NativeScriptApplication" metadata (move it to more appropriate place)
286286
auto node = MetadataNode::GetOrCreate("com/tns/NativeScriptApplication");
287-
auto appInstance = node->CreateJSWrapper(isolate);
287+
auto appInstance = g_objectManager->GetJsObjectByJavaObject(AppJavaObjectID);
288+
if(appInstance.IsEmpty())
289+
{
290+
appInstance = node->CreateJSWrapper(isolate);
291+
}
292+
288293
DEBUG_WRITE("Application object created id: %d", appInstance->GetIdentityHash());
289294

290295
auto implementationObject = args[0]->ToObject();

src/src/com/tns/NativeScriptApplication.java

Lines changed: 74 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -959,83 +959,96 @@ private boolean hasErrorIntent()
959959

960960
return hasErrorIntent;
961961
}
962+
963+
private boolean initializing;
962964

963965
public void onCreate()
964966
{
965-
System.loadLibrary("NativeScript");
966-
967-
Logger logger = new LogcatLogger(BuildConfig.DEBUG, this);
968-
967+
if(this.initializing)
968+
{
969+
// This may happen if the app.init object is not populated while accessing the Application object
970+
return;
971+
}
972+
969973
boolean showErrorIntent = hasErrorIntent();
970-
if (!showErrorIntent)
974+
if(showErrorIntent)
971975
{
972-
appInstance = this;
973-
974-
Thread.UncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, this);
976+
return;
977+
}
978+
979+
this.initializing = true;
980+
981+
System.loadLibrary("NativeScript");
982+
Logger logger = new LogcatLogger(BuildConfig.DEBUG, this);
983+
984+
appInstance = this;
975985

976-
prepareAppBuilderCallbackImpl(logger, exHandler);
986+
Thread.UncaughtExceptionHandler exHandler = new NativeScriptUncaughtExceptionHandler(logger, this);
977987

978-
Thread.setDefaultUncaughtExceptionHandler(exHandler);
988+
prepareAppBuilderCallbackImpl(logger, exHandler);
979989

980-
// TODO: refactor
981-
ExtractPolicy extractPolicy = (appBuilderCallbackImpl != null)
982-
? appBuilderCallbackImpl.getExtractPolicy()
983-
: new DefaultExtractPolicy(logger);
984-
boolean skipAssetExtraction = Util.runPlugin(logger, this);
985-
if (!skipAssetExtraction)
986-
{
987-
new AssetExtractor(null, logger).extractAssets(this, extractPolicy);
988-
}
990+
Thread.setDefaultUncaughtExceptionHandler(exHandler);
989991

990-
if (appBuilderCallbackImpl != null)
991-
{
992-
appBuilderCallbackImpl.onCreate(this);
993-
}
992+
// TODO: refactor
993+
ExtractPolicy extractPolicy = (appBuilderCallbackImpl != null)
994+
? appBuilderCallbackImpl.getExtractPolicy()
995+
: new DefaultExtractPolicy(logger);
996+
boolean skipAssetExtraction = Util.runPlugin(logger, this);
997+
if (!skipAssetExtraction)
998+
{
999+
new AssetExtractor(null, logger).extractAssets(this, extractPolicy);
1000+
}
9941001

995-
if (NativeScriptSyncService.isSyncEnabled(this))
996-
{
997-
NativeScriptSyncService syncService = new NativeScriptSyncService(logger, this);
1002+
if (appBuilderCallbackImpl != null)
1003+
{
1004+
appBuilderCallbackImpl.onCreate(this);
1005+
}
9981006

999-
syncService.sync();
1000-
syncService.startServer();
1007+
if (NativeScriptSyncService.isSyncEnabled(this))
1008+
{
1009+
NativeScriptSyncService syncService = new NativeScriptSyncService(logger, this);
10011010

1002-
// preserve this instance as strong reference
1003-
// do not preserve in NativeScriptApplication field inorder to make the code more portable
1004-
Platform.getOrCreateJavaObjectID(syncService);
1005-
}
1006-
else
1007-
{
1008-
if (logger.isEnabled())
1009-
{
1010-
logger.write("NativeScript LiveSync is not enabled.");
1011-
}
1012-
}
1011+
syncService.sync();
1012+
syncService.startServer();
10131013

1014-
String appName = this.getPackageName();
1015-
File rootDir = new File(this.getApplicationInfo().dataDir);
1016-
File appDir = this.getFilesDir();
1017-
1018-
ClassLoader classLoader = this.getClassLoader();
1019-
File dexDir = new File(rootDir, "code_cache/secondary-dexes");
1020-
String dexThumb = null;
1021-
try
1022-
{
1023-
dexThumb = Util.getDexThumb(this);
1024-
}
1025-
catch (NameNotFoundException e)
1014+
// preserve this instance as strong reference
1015+
// do not preserve in NativeScriptApplication field inorder to make the code more portable
1016+
Platform.getOrCreateJavaObjectID(syncService);
1017+
}
1018+
else
1019+
{
1020+
if (logger.isEnabled())
10261021
{
1027-
if (logger.isEnabled())
1028-
logger.write("Error while getting current proxy thumb");
1029-
e.printStackTrace();
1022+
logger.write("NativeScript LiveSync is not enabled.");
10301023
}
1031-
ThreadScheduler workThreadScheduler = new WorkThreadScheduler(new Handler(Looper.getMainLooper()));
1032-
// TODO: Refactor these 11 method parameters!!! E.g. create Settings abstract object and add default implementation object
1033-
Platform.init(this, workThreadScheduler, logger, appName, null, rootDir, appDir, classLoader, dexDir, dexThumb);
1034-
Platform.runScript(new File(appDir, "internal/prepareExtend.js"));
1035-
Platform.run();
1024+
}
10361025

1037-
onCreateInternal();
1026+
String appName = this.getPackageName();
1027+
File rootDir = new File(this.getApplicationInfo().dataDir);
1028+
File appDir = this.getFilesDir();
1029+
1030+
ClassLoader classLoader = this.getClassLoader();
1031+
File dexDir = new File(rootDir, "code_cache/secondary-dexes");
1032+
String dexThumb = null;
1033+
try
1034+
{
1035+
dexThumb = Util.getDexThumb(this);
1036+
}
1037+
catch (NameNotFoundException e)
1038+
{
1039+
if (logger.isEnabled())
1040+
logger.write("Error while getting current proxy thumb");
1041+
e.printStackTrace();
10381042
}
1043+
ThreadScheduler workThreadScheduler = new WorkThreadScheduler(new Handler(Looper.getMainLooper()));
1044+
// TODO: Refactor these 11 method parameters!!! E.g. create Settings abstract object and add default implementation object
1045+
Platform.init(this, workThreadScheduler, logger, appName, null, rootDir, appDir, classLoader, dexDir, dexThumb);
1046+
Platform.runScript(new File(appDir, "internal/prepareExtend.js"));
1047+
Platform.run();
1048+
1049+
onCreateInternal();
1050+
1051+
this.initializing = false;
10391052
}
10401053

10411054
private void prepareAppBuilderCallbackImpl(Logger logger, UncaughtExceptionHandler exHandler)
@@ -2166,4 +2179,4 @@ else if (name.equals("unregisterReceiver"))
21662179
private AppBuilderCallback appBuilderCallbackImpl;
21672180

21682181
private final String logTag = "NativeScriptApplication";
2169-
}
2182+
}

0 commit comments

Comments
 (0)