Question

I've got a problem... (Really? :-) )

What I want to do?

I need to make a native plugin to launch a MapActivity (to show POI, show itineraries, ...), and when the mapView is closed with some conditions, I have to load a particular html page.

So :

.HTML Page with JS Events -> .JS PlugIn -> .Java PlugIn -(launch)-> .Java MapActivity

And when my MapActivity is finished :

MapActivity send [Status.ok] or [message] X-> .Java PlugIn -> .JS CallBack -> Load new page

What's the problem?

But unfortunately, I have this flow :

-> .java Plugin Execute : { Launch(MapActivity); return Response }

and then my mapactivity is launched, but my callback is already passed without any data.

Another problem : PhoneGap seem's to cancel the onActivityResult(), it's impossible to get callback datas in the MapViewPlugin.java

The code?

mapview.js - Plugin

Problem with callBack which is called before the MapActivity is launched. The sleep(2000) is a workaround to show the new page after the MapView is shown. But it's not a good issue!

MapView.prototype.showItineraryMapView = function(itineraryEncoded, startLat,
        startLong, startTitle, startDesc, finishLat, finishLong, finishTitle,
        finishDesc, callBack) {
    PhoneGap.exec(function() {
        sleep(2000);
        callBack();
    }, function(){} , "MapView", "show_itinerary", [ itineraryEncoded, startLat, startLong,
            startTitle, startDesc, finishLat, finishLong, finishTitle,
            finishDesc ]);
};

function sleep(ms) {
    var dt = new Date();
    dt.setTime(dt.getTime() + ms);
    while (new Date().getTime() < dt.getTime());
}

MapViewPlugin.java - Plugin

@Override
public PluginResult execute(String action, JSONArray data, String callbackId)
{
    Log.i("MapViewPlugin", "Début MapViewPlugin");

    PluginResult result = null;
    if (action.equals(ACTION_SHOW_ITINERARY))
    {
        Log.i("MapViewPlugin", "showItineraryMapView");
        try
        {
            return this.showItineraryMapView(data.getString(0), data.getInt(1), data.getInt(2), data.getString(3), data.getString(4), data.getInt(5), data.getInt(6), data.getString(7), data.getString(8));
        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
    }
    else
    {
        result = new PluginResult(Status.INVALID_ACTION);
        Log.d("MapViewPlugin", "Invalid action : " + action + " passed");
    }
    return result;
}
private PluginResult showItineraryMapView(String encoded_itinerary, int startLat, int startLong, String startTitle, String startDesc, int finishLat, int finishLong, String finishTitle, String finishDesc)
{
    Log.i("MapViewPlugin", "Start showMapView()");
    Intent iMapView = new Intent(ctx, MapViewActivity.class);
    iMapView.putExtra(MODE, MODE_ITINERARY);
    iMapView.putExtra(ITINERARY_ENCODED, encoded_itinerary);
    iMapView.putExtra(START_LAT, startLat);
    iMapView.putExtra(START_LONG, startLong);
    iMapView.putExtra(START_TITLE, startTitle);
    iMapView.putExtra(START_DESC, startDesc);
    iMapView.putExtra(FINISH_LAT, finishLat);
    iMapView.putExtra(FINISH_LONG, finishLong);
    iMapView.putExtra(FINISH_TITLE, finishTitle);
    iMapView.putExtra(FINISH_DESC, finishDesc);

    Log.i("MapViewPlugin", "Launching intent...");

    ctx.startActivity(iMapView);

    return new PluginResult(Status.OK);
}

I don't know if it's the good solution, I am probably in the wrong way. Can anybody help me?

It's my first stackoverflow question so if you want more precision or if I am not enough precise, please ask me more details.

Thank's!

Was it helpful?

Solution

It's not quite right. Here are a few changes that should get you unstuck. First you'll need to save the callbackId that is passed into execute. Then in your execute method call showItineraryMapView() but don't get it to return a PluginResult have it return void.

Immediately after you make the call to showItineraryMapView() do this:

PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
return r;

Then in showItineraryMapView() you want to call ctx.startActivityForResult() instead of ctx.startActivity() and remove the line immediately afterwards that returns a new PluginResult.

Then you'll need to over-ride the onActivityResult() method. This is where you'll get your information from MapView. Then you end up calling:

this.success(new PluginResult(PluginResult.Status.OK, myData), this.callbackId);

where "myData" is the information you want to pass back to the JavaScript side and "this.callbackId" is the callback ID you stored in the execute method.

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