Nativescript: application.android.foregroundActivity is undefined

Created on 21 Jan 2018  路  11Comments  路  Source: NativeScript/NativeScript

_From @debevv on January 20, 2018 18:25_

Tell us about the problem

I really don't know if this is intended behaviour, but on both my application and a fresh javascript template the foregroundActivity member of application.android is always undefined. I noticed it because
this breaks nativescript-plugin-google-places.

Please provide the following version numbers that your issue occurs with:

  • CLI: 3.4.1
  • Cross-platform modules: 3.4.0
  • Runtime(s): 3.4.1
  • Plugin(s):
         "moment": "^2.19.3",
    "nativescript-facebook": "^2.1.0",
    "nativescript-plugin-google-places": "^1.4.1",
    "nativescript-pro-ui": "^3.3.0",
    "nativescript-theme-core": "^1.0.4",
    "tns-core-modules": "^3.4.0"

I'm testing the app inside an Android Oreo emulator.

Please tell us how to recreate the issue in as much detail as possible.

  • Create a new project (`tns create Test')
  • console.log(application.android.foregroundActivity) in app/main-page.js prints undefined

_Copied from original issue: NativeScript/android-runtime#927_

backlog android

Most helpful comment

Hello All,

After additional consideration, we suggest unmarking startActivity as deprecated; foregroundActivityremains unchanged - it is available on application.resumeEvent.

All 11 comments

@debevv the template application is hooked on the navigatingTo lifecycle event.
To get a reference to the foregroundActivity you will need navigatedTo event to be fired.
Otherwise, you can use the exposed startActiivity if you need to get an activity reference earlier

For example:

var application = require("application");

function onNavigatingTo(args) {
    console.log("============= onNavigatingTo =============");
    console.log("foregroundActivity: " + application.android.foregroundActivity); // undefined
    console.log("startActivity: " + application.android.startActivity); // com.tns.NativeScriptActivity@dbcd659
}
exports.onNavigatingTo = onNavigatingTo;

function onLoaded(args) {
    console.log("============= onLoaded =============");
    console.log("foregroundActivity: " + application.android.foregroundActivity); // undefined
    console.log("startActivity: " + application.android.startActivity); // com.tns.NativeScriptActivity@dbcd659
}
exports.onLoaded = onLoaded;

function onNavigatedTo(args) {
    console.log("============= onNavigatedTo =============");
    console.log("foregroundActivity: " + application.android.foregroundActivity); // com.tns.NativeScriptActivity@dbcd659
    console.log("startActivity: " + application.android.startActivity); // com.tns.NativeScriptActivity@dbcd659
}
exports.onNavigatedTo = onNavigatedTo;

Demonstration application can be found here

Logged an issue in the plugin's repository - closing this as related.

As startActivity is marked as deprecated in the source-code and with NativeScript 3.4.x we have introduced a change in the lifecycles that supportsforegroundActivity I am reopening this issue

I think this isn't necessarily a problem, as long it's made clear in the documentation that this member wil be usable only after the 'navigatedTo' event (my application is hooked to the 'onLoaded' one and it's not working)

@debevv For plugins it's a problem as they can't hook into the navigatedTo event.

@EddyVerbruggen the foregroundActivity is present in navigatedTo (but not in loaded)

Ok after looking at Android activity lifecycles we can consider setting the foregroundActivity to be set in onActivityCreated event . As the lifecycleCallbacks are attached to the native app runtime this would also mean that even if we have two activities (e.g. one for the splash screen and one for the main app) the lifecycleCallbacks are attached to the native app and the foregroundActivity value will be updated as expected.

And we can still deprecate the startActivity

ping @vchimev

TODO:

  • set foregroundActivity on onCreate(), onStart(), onResume() events

@NickIliev @vchimev Those changes would be very useful, thank you!

Hello All,

After additional consideration, we suggest unmarking startActivity as deprecated; foregroundActivityremains unchanged - it is available on application.resumeEvent.

That's perfectly fine with me as well.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings