Marinos K
Marinos K

Reputation: 1819

onMapReady not always called

I have three activities:

MainActivity:

public void start_main_map(View view) {
        Intent intent = new Intent(this, com.example.MainMap.class);
        startActivity(intent);
}

MainMap:

protected GoogleApiClient mGoogleApiClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate");
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
     mapFragment.getMapAsync(this);
}

@Override
protected void onStart() {
    Log.i(TAG, "onStart");
    mGoogleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    Log.i(TAG, "onStop");
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
protected void onPause() {
    Log.i(TAG, "onPause");
    super.onPause();
}

@Override
public void onResume() {
    Log.i(TAG, "onResume");
    super.onResume();
}

@Override
public void onMapReady(GoogleMap googleMap) {
    Log.i(TAG, "onMapReady");
    // do some serious work here
}

Guide:

private void implement_back_button() {
    final Intent intent = new Intent(this, com.example.MainMap.class);
    back = (Button) findViewById(R.id.back_button);
    back.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(intent);
        }
    });
}

Whenever I go from MainActivity to MainMap I get:

I/MainMap: onCreate
I/MainMap: onStart
I/MainMap: onResume
I/MainMap: onMapReady

Yet, whenever I go from Guide to MainMap (either using the back_button or pressing the back key in my cellphone) I only get:

I/MainMap: onCreate
I/MainMap: onStart
I/MainMap: onResume

No, onMapReady - so the "serious work" is never addressed. I cannot understand this behaviour. How can ensure that onMapReady is called in all cases?

Upvotes: 0

Views: 2514

Answers (3)

Daniel Nugent
Daniel Nugent

Reputation: 43322

Finishing the Map Activity is a bit heavy handed. You should be able to re-use the GoogleMap reference if the Google Map Activity was resumed.

Something like this should work:

protected GoogleApiClient mGoogleApiClient;
protected SupportMapFragment mapFragment;
protected GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate");
    mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

@Override
protected void onStart() {
    Log.i(TAG, "onStart");
    mGoogleApiClient.connect();
    super.onStart();
}

@Override
protected void onStop() {
    Log.i(TAG, "onStop");
    mGoogleApiClient.disconnect();
    super.onStop();
}

@Override
public void onResume() {
    Log.i(TAG, "onResume");
    super.onResume();

    //Added:
    if (mMap == null) {
      mapFragment.getMapAsync(this);
    } else {
      doSomeSeriousWork();
    }
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    Log.i(TAG, "onMapReady");
    doSomeSeriousWork();
}

public void doSomeSeriousWork() {
    // do some serious work here
}

Upvotes: 5

Ferdous Ahamed
Ferdous Ahamed

Reputation: 21736

From MainActivity to MainMap:

onMapReady() called, because MainMap created for the first time.

From Guide to MainMap:

onMapReady() was not called, because you are just returning back to MainMap and MainMap alreday exist in Activity stack and its Map is ready.

Solution:

Finish MainMap before starting Guide Activity.

Upvotes: 1

Ajay Shrestha
Ajay Shrestha

Reputation: 2455

When you pressed back from other Activity, Map Activity class just resume. And as map is already loaded before so onMapReady will not get called again.

To fix that, just finish the Map Activity before start other activity from Map Activity

Like this

Intent intent  = new Intent(MapActivity.class, OtherActivity.class);

startActivity(intent);

MapActivity.this.finish();

Upvotes: 1

Related Questions