onMapReady 并不总是被调用

Mar*_*s K 0 java android google-maps-android-api-2

我有三项活动:

主要活动:

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

主地图:

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
}
Run Code Online (Sandbox Code Playgroud)

指导:

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);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

每当我从MainActivityMainMap我得到:

I/MainMap: onCreate
I/MainMap: onStart
I/MainMap: onResume
I/MainMap: onMapReady
Run Code Online (Sandbox Code Playgroud)

然而,每当我从 到 时GuideMainMap使用back_button或按手机中的后退键),我只会得到:

I/MainMap: onCreate
I/MainMap: onStart
I/MainMap: onResume
Run Code Online (Sandbox Code Playgroud)

不,onMapReady-所以“严肃的工作”从未得到解决。我无法理解这种行为。如何确保onMapReady在所有情况下都被调用?

Dan*_*ent 5

完成地图活动有点繁琐。如果 Google 地图活动恢复,您应该能够重新使用 GoogleMap 引用。

像这样的东西应该有效:

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
}
Run Code Online (Sandbox Code Playgroud)