gpt4 book ai didi

java - onMapReady 并不总是被调用

转载 作者:行者123 更新时间:2023-12-01 17:57:28 25 4
gpt4 key购买 nike

我有三项 Activity :

主要 Activity :

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

主 map :

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
}

指南:

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);
}
});
}

每当我从 MainActivityMainMap 时,我都会得到:

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

但是,每当我从 Guide 转到 MainMap(使用 back_button 或按手机中的后退键)时,我只会得到:

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

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

最佳答案

完成 map Activity 有点繁琐。如果 Google map Activity 恢复,您应该能够重新使用 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
}

关于java - onMapReady 并不总是被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43549574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com