title: animateCamera android not working when returning back to activity
body: <p>I am struggling with animateCamera method of the Android Google Maps API, what is happening is that it only runs once. When I come back to activity after some time its not working at all. I don't know why its not working as its working very first time when i open the activity but after that its not working at all, i debug the code and also check that if any error is thrown or not but no errors are catched. Even animateCamera method call is smoothly running but map is not getting animated to position after returning to activity again.</p>
  
  <pre><code>public class MapsActivity extends Activity {
  
      private static GoogleMap mGoogleMap = null;
      private static final String TAG = "MapsActivity";
      private Button mBtnStartRide, mBtnPauseRide, mBtnStopRide = null;
      private static TextView mTxtLatLong, mTxtTimer, mTxtTotalSize,
              mTxtSpeed = null;
      private static PolylineOptions mRectLine = null;
      // Storing the directions returned by the direcction api
      private static ArrayList&lt;LatLng&gt; mDirectionsPoints = new ArrayList&lt;LatLng&gt;();
      private SharedPreferences mPreferences = null;
      private MyBroadcastReceiver receiver = null;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity);
  
          receiver = new MyBroadcastReceiver();
          mPreferences = getSharedPreferences(Preferences.PREFERENCES,
                  Context.MODE_PRIVATE);
  
          mTxtLatLong = (TextView) findViewById(R.id.txtLatLong);
          mTxtTimer = (TextView) findViewById(R.id.txtTimer);
          mTxtTotalSize = (TextView) findViewById(R.id.txtDirectionsSize);
          mTxtSpeed = (TextView) findViewById(R.id.txtSpeed);
  
          mBtnStartRide = (Button) findViewById(R.id.btn_start_ride);
          mBtnPauseRide = (Button) findViewById(R.id.btn_pause_ride);
          mBtnStopRide = (Button) findViewById(R.id.btn_stop_ride);
  
          mBtnStartRide.setOnClickListener(btnStartRideClickListener);
          mBtnPauseRide.setOnClickListener(btnPauseRideClickListener);
          mBtnStopRide.setOnClickListener(btnStopRideClickListener);
  
          initilizeMap();
      }
  
      /**
       * Start Ride Button Click Listener
       */
      private OnClickListener btnStartRideClickListener = new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              if (mPreferences.getBoolean(Preferences.IS_RIDE_PAUSE, false)) {
                  if (RidingTimerService.getInstance() != null) {
                      RidingTimerService.getInstance().reStartRide();
                  }
              } else {
                  startService(new Intent(MapsActivity.this,
                          RidingTimerService.class));
              }
  
              mPreferences.edit()
                      .putBoolean(Preferences.IS_RIDE_STOPPED, false)
                      .commit();
  
              mBtnStartRide.setVisibility(View.GONE);
              mBtnPauseRide.setVisibility(View.VISIBLE);
              mBtnStopRide.setVisibility(View.VISIBLE);
          }
      };
  
      /**
       * Start Ride Button Click Listener
       */
      private OnClickListener btnPauseRideClickListener = new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              RidingTimerService.getInstance().pauseRide();
              mPreferences.edit().putBoolean(Preferences.IS_RIDE_PAUSE, true)
                      .commit();
              mBtnStartRide.setVisibility(View.VISIBLE);
              mBtnPauseRide.setVisibility(View.GONE);
              mBtnStopRide.setVisibility(View.VISIBLE);
          }
      };
  
      /**
       * Stop Ride Button Click Listener
       */
      private OnClickListener btnStopRideClickListener = new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              stopService(new Intent(MapsActivity.this,
                      RidingTimerService.class));
              mPreferences.edit().remove(Preferences.IS_RIDE_PAUSE).commit();
              mPreferences.edit()
                      .putBoolean(Preferences.IS_RIDE_STOPPED, true).commit();
              mDirectionsPoints.clear();
              mBtnStartRide.setVisibility(View.VISIBLE);
              mBtnPauseRide.setVisibility(View.GONE);
              mBtnStopRide.setVisibility(View.GONE);
          }
      };
  
      @Override
      protected void onResume() {
          super.onResume();
          registerReceiver(receiver, new IntentFilter(
                  Constants.BROADCAST_INTENT));
          initilizeMap();
  
          if (mPreferences.getBoolean(Preferences.IS_RIDE_STOPPED, false)) {
              // Show start button and gone Pause &amp; Stop both
              mBtnStartRide.setVisibility(View.VISIBLE);
              mBtnPauseRide.setVisibility(View.GONE);
              mBtnStopRide.setVisibility(View.GONE);
          } else if (mPreferences
                  .getBoolean(Preferences.IS_RIDE_PAUSE, false)) {
              mBtnStartRide.setVisibility(View.VISIBLE);
              mBtnPauseRide.setVisibility(View.GONE);
              mBtnStopRide.setVisibility(View.VISIBLE);
          } else {
              mBtnStartRide.setVisibility(View.GONE);
              mBtnPauseRide.setVisibility(View.VISIBLE);
              mBtnStopRide.setVisibility(View.VISIBLE);
          }
  
          setAllText();
      }
  
      @Override
      protected void onPause() {
          super.onPause();
          unregisterReceiver(receiver);
      }
  
      /**
       * Function to load map. If map is not created it will create it for you
       * */
      private void initilizeMap() {
          if (mGoogleMap == null) {
              mGoogleMap = ((MapFragment) getFragmentManager().findFragmentById(
                      R.id.map)).getMap();
              // check if map is created successfully or not
              if (mGoogleMap == null) {
                  Toast.makeText(getApplicationContext(),
                          "Sorry! unable to create maps", Toast.LENGTH_SHORT)
                          .show();
              } else {
                  mGoogleMap.setMyLocationEnabled(true);
                  mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
                  mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
                  mGoogleMap.getUiSettings().setCompassEnabled(true);
                  mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
                  mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
  
                  mRectLine = new PolylineOptions().width(6).color(Color.BLUE);
              }
          } else {
              mGoogleMap.setMyLocationEnabled(true);
              mGoogleMap.getUiSettings().setMyLocationButtonEnabled(true);
              mGoogleMap.getUiSettings().setRotateGesturesEnabled(true);
              mGoogleMap.getUiSettings().setCompassEnabled(true);
              mGoogleMap.getUiSettings().setZoomGesturesEnabled(true);
              mGoogleMap.getUiSettings().setZoomControlsEnabled(true);
  
              mRectLine = new PolylineOptions().width(6).color(Color.BLUE);
              animateCameraTo(mDirectionsPoints.get(0).latitude,
                      mDirectionsPoints.get(0).longitude);
          }
      }
  
      /**
       * Handler which handle the timer of ride
       */
      public static Handler mCountDownHandler = new Handler() {
          public void handleMessage(android.os.Message msg) {
              Log.i(TAG, msg.getData().getString("time"));
              mTxtTimer.setText("Timer - " + msg.getData().getString("time"));
          };
      };
  
      /**
       * @author Scorpion
       * 
       */
      private class MyBroadcastReceiver extends BroadcastReceiver {
  
          @Override
          public void onReceive(Context context, Intent intent) {
  
              mDirectionsPoints.add(new LatLng(intent.getExtras().getDouble(
                      Preferences.LATITUDE), intent.getExtras().getDouble(
                      Preferences.LONGITUDE)));
  
              setAllText();
  
              if (!mDirectionsPoints.isEmpty() &amp;&amp; mDirectionsPoints.size() == 1) {
                  mGoogleMap.addMarker(new MarkerOptions()
                          .position(mDirectionsPoints.get(0)).anchor(0.8f, 1.0f)
                          .title("Your Location"));
              }
  
              animateCameraTo(
                      intent.getExtras().getDouble(Preferences.LATITUDE),
                      intent.getExtras().getDouble(Preferences.LONGITUDE));
          }
  
      }
  
      /**
       * 
       */
      public void setAllText() {
          mTxtLatLong.setText("Lat - "
                  + mPreferences.getString(Preferences.LATITUDE, "0.0")
                  + ", Lng - "
                  + mPreferences.getString(Preferences.LONGITUDE, "0.0"));
  
          mTxtSpeed.setText("Speed - "
                  + mPreferences.getString(Preferences.SPEED, "0.0"));
  
          mTxtTotalSize.setText("Distance - "
                  + mPreferences.getString(Preferences.DISTANCE, "0.0"));
      }
  
      /**
       * Animate to position on Google Maps
       * 
       * @param lat
       * @param lng
       */
      private void animateCameraTo(final double lat, final double lng) {
          // Saving the points in a polyline
          for (int i = 0; i &lt; mDirectionsPoints.size(); i++) {
              // Elevation array
              mRectLine.add(mDirectionsPoints.get(i));
          }
  
          // Drawing the path on the map
          mGoogleMap.addPolyline(mRectLine);
  
          CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngZoom(
                  new LatLng(lat, lng), 17);
          mGoogleMap.animateCamera(cameraUpdate);
      }
  }
  </code></pre>
  
  <p>Also tried this :-</p>
  
  <pre><code>CameraPosition cameraPosition = new CameraPosition.Builder().target(
                  new LatLng(17.385044, 78.486671)).zoom(12).build();
  
  mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
  </code></pre>
  
  <p>What i wish is whenever user come back to this activity again, he should be able to see the path and location markers as well. I am storing all the lat long so that is not an issue for me, but things are not working with animateCamera and markers both right now. Using Google Maps v2 first time so not sure what exactly the problem is. Look forward to get some quick help or tips on it.</p>