Java 我不总是打电话来
我有三项活动: 主要活动:Java 我不总是打电话来,java,android,google-maps-android-api-2,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); } 主地图: protected GoogleApiClient mGoogleApiClient; @Override protected void onCreate(Bundle sav
public void start_main_map(View view) {
Intent intent = new Intent(this, com.example.MainMap.class);
startActivity(intent);
}
主地图:
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);
}
});
}
每当我从MainActivity
转到MainMap
时,我会得到:
I/MainMap: onCreate
I/MainMap: onStart
I/MainMap: onResume
I/MainMap: onMapReady
然而,每当我从Guide
转到MainMap
(使用back\u按钮或按手机上的back键)时,我只得到:
I/MainMap: onCreate
I/MainMap: onStart
I/MainMap: onResume
不,onMapReady
-因此“严肃的工作”从未被提及。我不能理解这种行为。如何确保在所有情况下都调用了onMapReady
?当您从其他活动中按下后退键时,映射活动类只需继续。而且由于地图在so onMapReady之前已经加载,因此不会再次调用
要解决这个问题,只需在从Map Activity启动其他活动之前完成Map活动
像这样
Intent intent = new Intent(MapActivity.class, OtherActivity.class);
startActivity(intent);
MapActivity.this.finish();
当您从其他活动中按下后退键时,映射活动类就可以继续了。而且由于地图在so onMapReady之前已经加载,因此不会再次调用
要解决这个问题,只需在从Map Activity启动其他活动之前完成Map活动
像这样
Intent intent = new Intent(MapActivity.class, OtherActivity.class);
startActivity(intent);
MapActivity.this.finish();
从MainActivity
到MainMap
:
调用了onMapReady()
,因为MainMap
是第一次创建的
从Guide
到MainMap
:
未调用onMapReady()
,因为您刚刚返回到MainMap
和MainMap
始终存在于活动stack
中,并且其Map
处于ready
状态
解决方案:
在开始Guide
活动之前,完成MainMap
。从MainMap
到MainMap
:
调用了onMapReady()
,因为MainMap
是第一次创建的
从Guide
到MainMap
:
未调用onMapReady()
,因为您刚刚返回到MainMap
和MainMap
始终存在于活动stack
中,并且其Map
处于ready
状态
解决方案:
在开始Guide
活动之前,先完成MainMap
。完成Map活动有点笨手笨脚。
如果Google地图活动恢复,您应该能够重新使用Google地图引用
像这样的方法应该会奏效:
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
}
完成地图活动有点费力。
如果Google地图活动恢复,您应该能够重新使用Google地图引用
像这样的方法应该会奏效:
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
}
希望这有帮助你的问题不清楚。你的意思是如果你从主活动开始映射活动,onMapReady调用,但如果你从其他活动返回映射活动,onMapReady不调用?是的,这正是我的意思检查我的答案below@Praveen,一点帮助都没有-我已经在我的课上实现了在mapreadycallback上
,希望这个帮助你的问题不清楚。你的意思是如果你从主活动开始映射活动,onMapReady调用,但如果你从其他活动返回映射活动,onMapReady不调用?是的,这正是我的意思检查我的答案below@Praveen,一点帮助都没有-我的类中已经有在mapreadycallback上实现了