devendra.1.kumar

problem with graphics layer when running activities as apllication.

Discussion created by devendra.1.kumar on Mar 29, 2012
Latest reply on May 16, 2012 by Gwen_roy
Hi Guys
I am new to android development.
I made 5 activities with geo processing in them.
when running individualy its fine.
but when i used one activity as menu for all 5 GP Activities and navigating to them via intents, for the very first time the graphics layer is visible but for putting a point, but as soon as i press back button and again navigate to any of the Activities, on compilation side every thing is going fine, no exceptions but  no graphics are visible on graphics layer....

I am pasting code for one GP activity....
please help me i dont kno what i am doing wrong...


package com.esri.android.sample;

import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import com.esri.android.map.GraphicsLayer;
import com.esri.android.map.MapView;
import com.esri.android.map.ags.ArcGISTiledMapServiceLayer;
import com.esri.android.map.event.OnSingleTapListener;
import com.esri.core.geometry.Geometry;
import com.esri.core.geometry.Point;
import com.esri.core.map.Graphic;
import com.esri.core.symbol.SimpleFillSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol;
import com.esri.core.symbol.SimpleMarkerSymbol.STYLE;
import com.esri.core.tasks.ags.geoprocessing.GPFeatureRecordSetLayer;
import com.esri.core.tasks.ags.geoprocessing.GPParameter;
import com.esri.core.tasks.ags.geoprocessing.GPResultResource;
import com.esri.core.tasks.ags.geoprocessing.GPString;
import com.esri.core.tasks.ags.geoprocessing.Geoprocessor;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Handler.Callback;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;

public class DriveTimes extends Activity{

protected static final int CLOSE_LOADING_WINDOW = 0;
protected static final int CANCEL_LOADING_WINDOW = 1;
private MapView map = null;
private ArrayList<GPParameter> params;
private Geoprocessor gp;
private GraphicsLayer gLayer;
private ProgressDialog dialog = null;
private Timer cancelDriveTimes = new Timer();
private ImageButton delete, go;
private Point mappoint;


private Handler uiHandler = new Handler(new Callback() {
  public boolean handleMessage(Message msg) {
   switch (msg.what) {
   case CLOSE_LOADING_WINDOW:
    if (dialog != null) {
     dialog.dismiss();
    }
    cancelDriveTimes.cancel();
    break;
   case CANCEL_LOADING_WINDOW:
    if (dialog != null) {
     dialog.dismiss();
    }
    Toast toast = Toast.makeText(DriveTimes.this,
      "Taking too long to execute, canceled", Toast.LENGTH_SHORT);
    toast.show();
    break;
   }
   return false;
  }

});

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.drive_times);
 
  map = (MapView)findViewById(R.id.map);
  // Add dynamic layer to MapView
  map.addLayer(new ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/ESRI_StreetMap_World_2D/MapServer")); 
 
 
  gLayer = new GraphicsLayer();
  map.addLayer(gLayer);
 
  delete = (ImageButton)findViewById(R.id.deletebutton);
  go = (ImageButton)findViewById(R.id.gobutton);
 
  Toast.makeText(this, "Single tap on the map", Toast.LENGTH_SHORT).show();

  /**
   * Clear all graphics from the graphics layer. The method is called when
   * the trash can button is clicked by the user.
   */
  delete.setOnClickListener(new OnClickListener() {
  
   public void onClick(View v) {
    gLayer.removeAll();
   
   }
  });
  delete.setOnClickListener(new OnClickListener() {
   public void onClick(View arg0) {
    gLayer.removeAll();
   }
  });
 
  /**
   * In response to the clicking of the arrow button
   */
 
  go.setOnClickListener(new OnClickListener() {
  
   public void onClick(View v) {
    start(mappoint);
   
   }
  });
  go.setOnClickListener(new OnClickListener() {
   public void onClick(View arg0) {
    start(mappoint);
   }
  });

  map.setOnSingleTapListener(new OnSingleTapListener() {
   private static final long serialVersionUID = 1L;

   public void onSingleTap(float x, float y) {
    mappoint = map.toMapPoint(x, y);
    Graphic g = new Graphic(mappoint,new SimpleMarkerSymbol(Color.RED, 10, STYLE.CIRCLE));
    gLayer.addGraphic(g);
   }
  });
 
  //Retrieve the non-configuration instance data that was previously returned.
  Object init = getLastNonConfigurationInstance();
  if (init != null) {
   map.restoreState((String) init);
  } 
 
}

class DriveTimesQuery extends AsyncTask<ArrayList<GPParameter>, Void, GPParameter[]> {

  GPParameter[] outParams = null;

  @Override
  protected void onPostExecute(GPParameter[] result) {
   if (outParams == null)
    return;
   for (int i = 0; i < outParams.length; i++) {
    if (outParams[i] instanceof GPFeatureRecordSetLayer) {

     GPFeatureRecordSetLayer fsl = (GPFeatureRecordSetLayer) outParams[i];
     SimpleFillSymbol buffer1=new SimpleFillSymbol(Color.GRAY);
     SimpleFillSymbol buffer2=new SimpleFillSymbol(Color.BLUE);
     SimpleFillSymbol buffer3=new SimpleFillSymbol(Color.RED);
     buffer1.setAlpha(5);
     buffer2.setAlpha(5);
     buffer3.setAlpha(5);
     int count=0;
     for (Graphic feature : fsl.getGraphics()) {
      if(count==0)
      {
       Graphic g = new Graphic(feature.getGeometry(),buffer1);
       gLayer.addGraphic(g);
      
      }
      else if(count==1)
      {
       Graphic g1 = new Graphic(feature.getGeometry(),buffer2);
          gLayer.addGraphic(g1);
         
         }
      else if(count==2)
      {
       Graphic g2 = new Graphic(feature.getGeometry(),buffer3);
          gLayer.addGraphic(g2);
         
      }
     
      count=count + 1;
     }
    }
   }
   uiHandler.sendEmptyMessage(CLOSE_LOADING_WINDOW);
  }

  @Override
  protected GPParameter[] doInBackground(ArrayList<GPParameter>... params1) {

   gp = new Geoprocessor("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Network/ESRI_DriveTime_US/GPServer/CreateDriveTimePolygons");
   gp.setOutSR(map.getSpatialReference().getID());
   try {
    GPResultResource rr = gp.execute(params1[0]);
    outParams = rr.getOutputParameters();
   } catch (Exception e) {
    e.printStackTrace();
   }
   return outParams;
  }
}

/**
  * 1) Create the GP object by passing it the url. 2) Create the input
  * parameters and add to the GPParameter object. 3) Pass these params to the
  * execute method.
  *
  * @param mappoint
  *            - point selected on the map by the user
  */
public void start(Point mappoint) {
  // First input parameter
  GPFeatureRecordSetLayer gpf = new GPFeatureRecordSetLayer("Input_Location");
  gpf.setSpatialReference(map.getSpatialReference());
  gpf.setGeometryType(Geometry.Type.Point);
  // Add the point selected by the user
  Graphic f = new Graphic(mappoint,new SimpleMarkerSymbol(Color.RED,25,STYLE.DIAMOND));
  gpf.addGraphic(f);

  // Second input parameter
  GPString gps=new GPString("Drive_Times");
  gps.setValue("2 5 7");
  //GPLinearUnit gpl = new GPLinearUnit("Days");
  //gpl.setUnits("esriDouble");
  //gpl.setDistance(365);

  // Add params
  params = new ArrayList<GPParameter>();
  params.add(gpf);
  params.add(gps);

  try {
   dialog = ProgressDialog.show(DriveTimes.this, "", "Loading. Please wait...", true, true);
   new DriveTimesQuery().execute(params);
   cancelDriveTimes = new Timer();
   cancelDriveTimes.schedule(new TimerTask() {

    @Override
    public void run() {
     uiHandler.sendEmptyMessage(CANCEL_LOADING_WINDOW);
    }
   }, 60000);
  } catch (Exception e) {
   e.printStackTrace();
  }
}

@Override
protected void onPause() {
  super.onPause();
  map.pause();
}

@Override 
protected void onResume() {
  super.onResume();
  map.unpause();
}

@Override
public void onBackPressed() {
  // TODO Auto-generated method stub
  this.finish();
  super.onBackPressed();
}


}

Outcomes