[TIMOB-26228] Android: Cannot get current activity in native module when using 7.3.0+
| GitHub Issue | n/a | 
|---|---|
| Type | Bug | 
| Priority | Critical | 
| Status | Closed | 
| Resolution | Invalid | 
| Resolution Date | 2018-07-25T01:50:41.000+0000 | 
| Affected Version/s | n/a | 
| Fix Version/s | n/a | 
| Components | Android | 
| Labels | android | 
| Reporter | Hans Knöchel | 
| Assignee | Joshua Quick | 
| Created | 2018-07-22T13:48:45.000+0000 | 
| Updated | 2018-08-06T17:37:03.000+0000 | 
Description
	I have a module where I want to configure an SDK by passing the current activity to it automatically  on app start. We have the 
onCreate method via the @Kroll.onCreate annotation, but neither of the following methods worked there:
- getActivity() (because of the static context)
- TiApplication.getAppRootOrCurrentActivity() errors: 
{quote}
No valid root or current activity found for application instance
{quote}
- TiApplication.getInstance().getCurrentActivity(): Same as above
I also tried invoking them in the constructor of the module but that did not work either. I am trying to prevent a manually initialize() call from the module, so I am wondering if there is a better approach here.
Update: It seems to not crash with:
@Override public void onStart(Activity activity) { // Use "activity" from here }Update 2: It actually works fine in SDK 7.2.0 and does not any warning. Considering this a 7.3.0 regression, updating the ticket accordingly.Titanium does not have a Java
@Kroll.onCreateannotation. Perhaps you mean@Kroll.onAppCreateinstead? If so, then that method will be called when the Android JavaApplicationinstance gets created during a cold-start, but before an AndroidActivitygets created. This is by Google's design, not ours. You cannot fetch anActivityon app startup because no UI has been created yet. So, yes, the getters for an activity should returnnullat this point. This is the correct behavior. What you can do instead is override the JavaKrollModule.onStart(Activity)method like [~hknoechel] has suggested. https://docs.appcelerator.com/module-apidoc/latest/android/org/appcelerator/kroll/KrollModule.html#onStart(android.app.Activity) Alternatively, only call theTiApplication.getAppRootOrCurrentActivity()when your module's APIs have been called by JavaScript. An activity is guaranteed to be available then because the JavaScript runtime is created when the splash screen activity gets displayed. The splash screen activity hosts the JavaScript runtime and when you close all activities the JavaScript runtime is terminated. So, this is the best solution.This is not a regression. The warnings on startup are harmless. We've reduced the number of warnings on app startup in Titanium 7.4.0. We'll look into getting rid of the rest of the false-positive warnings in the future.
[~jquick] I am not feeling good resolving this already. I just tested a few of my other modules and they also spit out the error when accessing our activity and they are not called from
onCreate:Closing as invalid. If incorrect, please reopen.