Android - OnMyLocationChangeListener not work when I close and open again the activity

380 views Asked by At

In my application I'm using Maps API v2. I have an activity that contains a map:

<LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:gravity="center"
                android:orientation="vertical">

                <fragment
                    android:id="@+id/map"
                    android:name="com.google.android.gms.maps.SupportMapFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_gravity="center" />
            </LinearLayout>

When the activity starts, shows my location on the map and is updated using OnMyLocationChangeListener. Everything works perfectly, but I have a big problem:

When I open the activity works great, I can see the icon of the location in the notification bar enter image description here

If I push the home button (the square button) of the device, I leave the app, but not close it. When I reopen my application, continues showing and updating my location on the map this is correct, now comes the problem.

When i push the deviceĀ“s back button, i leave the app, but when I open it again, the location is no longer active (the location icon is not shown in the notification bar) and the map is not updated

I have tried many ways to do it, I read forums and tutorials, but without success. I do not know what is the problem.

I appreciate the help, I am very confused.

Thank you very much.

This is the code of my activity:

public class Home extends ActionBarActivity {

        public static GoogleMap mMap;

        //...

        private void setUpMapIfNeeded() {

        if (mMap == null) {
            mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
            if (mMap != null) {
                setUpMap();
            }
        }
    }       

    private void setUpMap() {

        mMap.setMyLocationEnabled(true);

        GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
            @Override
            public void onMyLocationChange(Location location) {
                LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());

                if (ll_ubicacion_mapa.getVisibility() == View.VISIBLE) {
                    ll_ubicacion_mapa.setVisibility(View.GONE);
                } else {
                }

                mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(loc, 16.0f));

                try {
                    indicadorDePosicion.remove();
                } catch (Exception e) {
                }

                indicadorDePosicion = mMap.addMarker(new MarkerOptions()
                        .flat(true)
                        .title(getResources().getString(R.string.TXestasAqui))
                        .icon(BitmapDescriptorFactory.fromResource(R.mipmap.ic_launcher))
                        .anchor(0.5f, 0.5f)
                        .position(new LatLng(location.getLatitude(), location.getLongitude())));

            }
        };

        mMap.setOnMyLocationChangeListener(myLocationChangeListener);
    }


      @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);

        setUpMapIfNeeded();

        //...
    }


    @Override
    protected void onResume() {
        super.onResume();
        setUpMapIfNeeded();


    }

    protected void onPause() {
        super.onPause();
    }

}
0

There are 0 answers