Titanium JIRA Archive
Titanium SDK/CLI (TIMOB)

[TIMOB-26228] Android: Cannot get current activity in native module when using 7.3.0+

GitHub Issuen/a
TypeBug
PriorityCritical
StatusClosed
ResolutionInvalid
Resolution Date2018-07-25T01:50:41.000+0000
Affected Version/sn/a
Fix Version/sn/a
ComponentsAndroid
Labelsandroid
ReporterHans Knöchel
AssigneeJoshua Quick
Created2018-07-22T13:48:45.000+0000
Updated2018-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.

Comments

  1. Hans Knöchel 2018-07-22

    Update: It seems to not crash with:
       	@Override
       	public void onStart(Activity activity) {
       		// Use "activity" from here
       	}
       
    But it still logs this:
       [ERROR] TiApplication: (main) [817,864] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [2,866] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [9,875] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [5,880] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [1,881] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [1,882] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [3,885] No valid root or current activity found for application instance
       [WARN]  TiAndroid: (main) [1,886] Application instance no longer available. Unable to get current activity.
       [ERROR] TiApplication: (main) [1,887] No valid root or current activity found for application instance
       [WARN]  TiAndroid: (main) [1,888] Application instance no longer available. Unable to get current activity.
       [WARN]  TiAndroid: (main) [0,888] Application instance no longer available. Unable to get current activity.
       [ERROR] TiApplication: (main) [4,892] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [7,899] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [2,901] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [1,902] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [0,902] No valid root or current activity found for application instance
       
    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.
  2. Joshua Quick 2018-07-25

    Titanium does not have a Java @Kroll.onCreate annotation. Perhaps you mean @Kroll.onAppCreate instead? If so, then that method will be called when the Android Java Application instance gets created during a cold-start, but before an Android Activity gets created. This is by Google's design, not ours. You cannot fetch an Activity on app startup because no UI has been created yet. So, yes, the getters for an activity should return null at this point. This is the correct behavior. What you can do instead is override the Java KrollModule.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 the TiApplication.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.
  3. Joshua Quick 2018-07-25

    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.
  4. Hans Knöchel 2018-07-25

    [~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:
       [ERROR] TiApplication: (main) [480,515] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [2,517] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [10,527] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [5,532] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [2,534] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [1,535] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [2,537] No valid root or current activity found for application instance
       [WARN]  TiAndroid: (main) [1,538] Application instance no longer available. Unable to get current activity.
       [ERROR] TiApplication: (main) [0,538] No valid root or current activity found for application instance
       [WARN]  TiAndroid: (main) [0,538] Application instance no longer available. Unable to get current activity.
       [WARN]  TiAndroid: (main) [0,538] Application instance no longer available. Unable to get current activity.
       [ERROR] TiApplication: (main) [1,539] No valid root or current activity found for application instance
       [WARN]  ContextImpl: Failed to ensure /dev/null/Android/data/com.appc.firebasedemo/cache: 400
       [ERROR] TiApplication: (main) [9,548] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [1,549] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [0,549] No valid root or current activity found for application instance
       [ERROR] TiApplication: (main) [1,550] No valid root or current activity found for application instance
       
  5. Eric Merriman 2018-08-06

    Closing as invalid. If incorrect, please reopen.

JSON Source