Question

I'm trying follow the example in this Worklight Tutorial, to send data back from the native page. However, when it returns from the native page, the argument (data) in the callback function (backFromNativePage(data)) is undefined. I'm new to working with Worklight, so perhaps I'm not doing this correctly? I was hoping someone could shed some light on what I might be doing wrong. Thanks!

Here's what some of my code looks like:

Login.java:

public class Login extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LoginWebViewClient client = new LoginWebViewClient(this);

        webView = (WebView) findViewById(R.id.login);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(client);
        webView.loadUrl(url);
    }

    public void setAppDataAndReturn(HashMap<String, String> dataList) {
        Intent returnData = new Intent();

        for (Map.Entry<String, String> item : dataList.entrySet()) {
            returnData.putExtra(item.getKey(), item.getValue());
        }

        setResult(RESULT_OK, returnData);
        finish();
    }
}

LoginWebViewClient:

public class LoginWebViewClient extends WebViewClient {
    private Login webAct;
    private HashMap<String, String> dataList;

    public LoginWebViewClient(Login webAct) {
        this.webAct = webAct;
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        this.dataList = getData(); //returns a HashMap    
        webAct.setAppDataAndReturn(dataList);
    }

AppInit.js:

var backFromNativePage = function(data) {
    WL.Logger.error("backFromNativeLoginPage: data= "+ data);
};

var showNativePage = function() {
    WL.NativePage.show('com.app.Login', backFromNativePage, {param: 'some value'});
};

By the way, I'm using Worklight version 6.1.0.00-20131219-1900 and I'm testing on an Android 4.3 phone.

Any help would be much appreciated!

Edit:

Following the suggestion from @IdanAdar, this is how I changed it to get it to work:

AppInit.js:

var showNativePage = function() {
    WL.NativePage.show('com.app.Login', function(data) {
        WL.Logger.error("backFromNativeLoginPage: data= "+ data);
    }, {param: 'some value'});
};
Was it helpful?

Solution

Based on the discussion in the comments: Try defining the callback function as a function instead of a variable.

function backFromNativePage(data) { 
    ...
}

OTHER TIPS

I tried most of your code (did not use the webclient part), as a new project and it works.

Can you download the Worklight sample and test it?

Most important, does the native page load for you, meaning, did you add the activity to android-manifest.xml?

Can you upload your eclipse project to somewhere so I can see it, and see whats wrong. This should really work without issues.

http://public.dhe.ibm.com/software/mobile-solutions/worklight/docs/v610/UsingNativePagesInHybridAppsProject.zip

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top