How can I access variable from a callback method on Android

864 views Asked by At

I have a class named MapViewManager, in it I have a method called navigate like below:

public class MapViewManager{
    ...
    public void navigate(double startX, double startY, double toX, double toY, long floorId, boolean flag) {
        final FeatureLayer featureLayer = new FeatureLayer("navigate");
        mapView.setLayerOffset(featureLayer);
        mapView.addLayer(featureLayer);

        final NavigateManager navigateManager = new NavigateManager();
        if (flag) {
            navigateManager.navigation(startX, startY, floorId, toX, toY, floorId);
        }else{
            navigateManager.clear();
        }

        navigateManager.setOnNavigateComplete(new NavigateManager.OnNavigateComplete() {
            @Override
            public void onNavigateComplete(NavigateManager.NavigateState navigateState,
                                       FeatureCollection featureCollection) {
                featureLayer.clearFeatures();
                featureLayer.addFeatures(featureCollection);

                for (int i=0;i<featureCollection.getSize();i++){
                    Feature feature = featureCollection.getFeature(i);
                    Coordinate coordinate = feature.getCentroid();
                    double x = coordinate.getX();
                    double y = coordinate.getY();   
                }
            }
        });
    }
}

I want to access the variable named coordinate in onNavigateComplete callback method from outside so that other classes can use the coordinate variable to do something. How can I make it?

2

There are 2 answers

3
Sahil Munjal On BEST ANSWER

Add one more parameter to navigate method.

public class MapViewManager{
    ...
    public void navigate(double startX, double startY, double toX, double toY, long floorId, boolean flag, NavigateManager.OnNavigateComplete navigation) {
        final FeatureLayer featureLayer = new FeatureLayer("navigate");
        mapView.setLayerOffset(featureLayer);
        mapView.addLayer(featureLayer);

        final NavigateManager navigateManager = new NavigateManager();
        if (flag) {
            navigateManager.navigation(startX, startY, floorId, toX, toY, floorId);
        }else{
            navigateManager.clear();
        }

        navigateManager.setOnNavigateComplete(navigation);
    }
}

Call method from other Class :

new MapViewManager().navigate(other paramter values, new NavigateManager.OnNavigateComplete() {
            @Override
            public void onNavigateComplete(NavigateManager.NavigateState navigateState,
                                       FeatureCollection featureCollection) {
                featureLayer.clearFeatures();
                featureLayer.addFeatures(featureCollection);

                for (int i=0;i<featureCollection.getSize();i++){
                    Feature feature = featureCollection.getFeature(i);
                    Coordinate coordinate = feature.getCentroid();
                    navigationPoints.add(coordinate);
                    double x = coordinate.getX();
                    double y = coordinate.getY();   
                }
            }
        });

Now you can access any object anywhere.

2
Salaudeen Abdulrahman On

You have to make it public by doing this:

Say your activity name is 'Main'

    ....
Public class Main extends AppCompatActivity{

Coordinate coor; 

//in the onNavigationComplete method do this

Coordinate coordinate = //..complete this;
coor = coordinate; 
…
…}