Close a new Activity in Cordova Plugin

Viewed 60

I created a plugin in cordova to open a new activity with a predetermined URL, which is working perfectly.

But I wanted to create a command to be able to close it, but I'm not able to.

When I use the AbrirHTML.close() function, nothing happens.

How do I resolve this?

AbrirHTML.js

var exec = require('cordova/exec');

function plugin() {

}

plugin.prototype.open = function (params) {
    exec(function (res) {
        console.log('params', params)
        console.log('res', res);
    }, function (err) {
        console.log(err);
    }, "AbrirHTML", "open", [params]);
}

plugin.prototype.close = function (params) {
    exec(function (res) {
        console.log('res', res);
    }, function (err) {
        console.log(err);
    }, "AbrirHTML", "close", null);

}

module.exports = new plugin();

plugin.xml

<?xml version='1.0' encoding='utf-8'?>
<plugin id="com.actionsistemas.abrirhtml" version="0.0.1" xmlns="http://apache.org/cordova/ns/plugins/1.0" xmlns:android="http://schemas.android.com/apk/res/android">
    <name>AbrirHTML</name>
    <js-module name="AbrirHTML" src="www/AbrirHTML.js">
        <clobbers target="AbrirHTML" />
    </js-module>
    <platform name="android">
        <config-file parent="/*" target="res/xml/config.xml">
            <feature name="AbrirHTML">
                <param name="android-package" value="com.actionsistemas.abrirhtml.AbrirHTML" />
            </feature>
        </config-file>
        <config-file target="AndroidManifest.xml" parent="/manifest/application">
            <activity android:label="Menu" android:name="com.actionsistemas.abrirhtml.NewActivity" android:theme="@style/Theme.Transparent"></activity>
        </config-file>
        <config-file parent="/*" target="AndroidManifest.xml"></config-file>
        <source-file src="src/android/AbrirHTML.java" target-dir="src/com/actionsistemas/abrirhtml" />
        <source-file src="src/android/NewActivity.java" target-dir="src/com/actionsistemas/abrirhtml" />
        <source-file src="src/android/activity_new.xml" target-dir="res/layout"/>
    </platform>
</plugin>

AbrirHTML.java

package com.actionsistemas.abrirhtml;

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaWebView;
import org.apache.cordova.CordovaInterface;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class AbrirHTML extends CordovaPlugin {

    public void initialize(CordovaInterface cordova, CordovaWebView webView) {
        super.initialize(cordova, webView);
    }

    @Override
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
        Context context = cordova.getActivity().getApplicationContext();
        if(action.equals("open")) {
            String url = args.getString(0);
            Log.e("*** URL ***", url);
            this.openNewActivity(context, url);
            return true;
        } else if(action.equals("close")){
            this.closeNewActivity(context);
            return true;
        }
        return false;
    }

    private void openNewActivity(Context context, String url) {
        Intent intent = new Intent(context, NewActivity.class);
        intent.putExtra("url", url);
        this.cordova.getActivity().startActivity(intent);
    }

    private void closeNewActivity(Context context) {
        //Intent intent = new Intent(context, NewActivity.class);
        this.cordova.getActivity().finish();
    }
}

NewActivity.java

package com.actionsistemas.abrirhtml;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.util.Log;

public class NewActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String package_name = getApplication().getPackageName();
        Intent intent = getIntent();
        String html = intent.getStringExtra("url");
        Log.e("*** HTML ***", html);
        setContentView(getApplication().getResources().getIdentifier("activity_new", "layout", package_name));
        WebView webView = (WebView) findViewById(getApplication().getResources().getIdentifier("web1", "id", package_name));  
        WebSettings settings = webView.getSettings();
        settings.setSupportMultipleWindows(true);
        settings.setJavaScriptCanOpenWindowsAutomatically(false);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
        settings.setCacheMode(2);
        webView.clearCache(true);
        webView.setBackgroundColor(0);
        webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
        //webView.loadUrl("file:///android_asset/www/menu.html");
        webView.loadUrl(html);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        String package_name = getApplication().getPackageName();
        WebView webView = (WebView) findViewById(getApplication().getResources().getIdentifier("web1", "id", package_name));  
        webView.destroy();
    }
}
0 Answers
Related