Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Errors extending NativeScriptActivity on Android #462

Closed
enchev opened this issue May 25, 2016 · 10 comments
Closed

Errors extending NativeScriptActivity on Android #462

enchev opened this issue May 25, 2016 · 10 comments
Assignees
Labels
Milestone

Comments

@enchev
Copy link

enchev commented May 25, 2016

From @chimon2000 on May 23, 2016 22:30

Versions

tns: 2.0.1
tns-core-modules: 2.0.1
tns-android: 2.0.0

Description

When attempting to extend NativeScriptActivity, I run into several issues:

var app = require("application");
var mainActivity = com.tns.NativeScriptActivity.extend({
    onCreate: function (savedState) {
        console.log('loaded');
        // call the base NativeScriptActivity.onCreate method
        // the "this" variable points to a NativeScriptActivity instance
        this.super.onCreate(savedState);

        // create a button and set it as the main content
        var button = new android.widget.Button(this);
        button.setText("Hello World");

        this.setContentView(button);
    }
});

var applicationInitObject = {
    getActivity: function (intent) {
        // this method is called whenever a new instance of NativeScriptActivity is about to be created
        return mainActivity;
    },
    onCreate: function () {
        // This is the first method called. Called from the android.app.Application.onCreate method.
    }
}

app.init(applicationInitObject);

I took the example from here, and I assume that the information is stale since it refers to bootstrap.js. Unfortunately, I can't find anymore documentation around the feature.

Copied from original issue: NativeScript/NativeScript#2166

@enchev
Copy link
Author

enchev commented May 25, 2016

From @chimon2000 on May 24, 2016 17:17

Further Details

What I am actually trying to do is extend the NativeScriptActivity to add an event for user interaction that I can handle on the JS side. I reviewed the blog post about SBG, but could not figure out how to override NativeScriptActivity in MyActivity.js. Doing the following

com.tns.NativeScriptActivity.extend({
    onCreate: function (bundle) {
        this.super.onCreate(bundle);
        console.log('overwrite');
    }
});

results in no MyActivity.java file being generated, and I get an error when the app loads.

I also attempted the following

var frameModule = require("ui/frame");
var mainActivity = Object.assign({
    onUserInteraction: function(){
        console.log('user interaction');
    }
}, frameModule)

android.app.Activity.extend("com.tns.MyActivity", mainActivity);

which results in just a blank screen being displayed. I'm sure that both of these fail because the generator is static (of course). Just not sure how to get the desired result of overriding/adding to the default activity.

@enchev
Copy link
Author

enchev commented May 25, 2016

From @vjoao on May 24, 2016 19:11

Maybe new it?

var frameModule = require("ui/frame");
var mainActivity = Object.assign({
    onUserInteraction: function(){
        console.log('user interaction');
    }
}, frameModule)

android.app.Activity.extend("com.tns.MyActivity", **new mainActivity()**);

@enchev
Copy link
Author

enchev commented May 25, 2016

From @chimon2000 on May 24, 2016 20:29

@vjoao I attempted that with just the default Activity new com.tns.NativeScriptActivity() with the same result.

I also tried removing the assign and just passing in mainActivity. In both cases MyActivity.java was not generated.

It appears that only passing the object directly into the method yields the expected outcome (makes sense because it is static).

@slavchev
Copy link

Hi @chimon2000

Firstly, let me acknowledge that current mechanism for extending activities is bit rough and generally requires some understanding how {N} works. While it is functional it is not developer friendly. Think of it as a first step in an iterative process. The good news is that last week I prepared an internal document, which is in review now, and I guess I will publish it as a github issue next week so the discussion continues in public. With that said, let me try to help.

You can extend an activity as follows:

var MyActivity = android.app.Activity.extend("com.example.MyActivity", {
  onCreate: function() {
    // body omitted
  }
});

During build-time the static binding generator will create new MyActivity.java file. You can find the file under <project_dir>/platforms/android/src/main/java/com/example directory. It is important to understand the role of the first argument ("com.example.MyActivity") of the extend function (see #283 for more info). So the first syntax use used

var mainActivity = com.tns.NativeScriptActivity.extend({
...

is not the correct one. In case you want to extend a class that you are going to use in AndroidManifest.xml you should always use extend with a.b.C where class a.b.C comes from an existing library. In the previous example I used android.app.Activity which comes from the Android API. Your second example

android.app.Activity.extend("com.tns.MyActivity", mainActivity);

uses the correct syntax. And indeed as you said you see blank activity. Currently this is the expected behavior because you don't provide any UI. Take a look at how we use this approach in our test app. Basically, we overwrite onCreate method and build manually a simple layout with a button.

When it comes to our modules we have slightly different implementation (please ignore TypeScript specifics). This implementation has understandings of the other modules classes like Application, Fragment, etc. which all together allow functionality like declaring UI in xml, navigation and so on. And you can see that the main activity is hard-coded in our project template.

Hopefully I managed to explain what is done until now. What is going next? As I said we are discussing internally the initial draft of a proposal that will allow any developer to extend activities in general, including the ones that comes from our modules implementation. Once we cut the rough corners we will post a issue with the proposal so it can be reviewed in public. I expect it to happen next week.

@emankovski
Copy link

+1
NativeScript is great to build simple stuff and has awesome infrastructure, with a lot of Javascript API available. But for Enterprise grade apps, we need billion other things, which will make its way, potentially, into NativeScript over the time. But the problems are here and now we need to resolve ... and ability to easily plug into into internal guts of Activity is extremely important!

@vjoao
Copy link

vjoao commented May 25, 2016

@emankovski Can you specify what do you need? Perhaps put it on https://nativescript.ideas.aha.io/ideas so people can see and vote for them to be included.

@emankovski
Copy link

emankovski commented May 25, 2016

@vjoao There are many scenarios can come up. One on them, we tried to tackle, is to hook on user interaction to track user activity on high level:

@Override
public void onUserInteraction() ...

As I said in previous post we have some specific requirement. That is custom timeout. I.e. user has its phone set to lock after 5 minutes but general corporate rules can say just 1 minute. Enterprise mentality is not to trust to user even on his own phone :) So we have to track timeout ourselves... We tried to hook on page level taps but they dont bubble up when controls handle gestures. So we cannot reset timer correctly. The fallback is to go to Native level and try to user Activity methods. We tried to find any way to allow bubbling for NativeScript UI events on a page level, but that could be a feature to consider to implement.

@chimon2000
Copy link

@slavchev I am glad to hear that you guys are aware of this. Looking forward to reviewing what you come up with!

@slavchev
Copy link

Guys, here is the first part of the proposal in question. It is related mostly to the folder structure as such. I will add link to the part related to the changes in our modules once it is ready.

@atanasovg atanasovg self-assigned this Jun 23, 2016
@atanasovg atanasovg added this to the 2.1 milestone Jun 23, 2016
@atanasovg
Copy link
Contributor

Closing this issue, please refer to #465 (comment) for further information on what we've enabled for 2.1

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants