【发布时间】:2017-04-22 03:11:00
【问题描述】:
我有三个活动:
主活动:
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_button 或按手机中的返回键)时,我只会得到:
I/MainMap: onCreate
I/MainMap: onStart
I/MainMap: onResume
不,onMapReady - 所以“严肃的工作”从未得到解决。我无法理解这种行为。如何保证onMapReady在所有情况下都被调用?
【问题讨论】:
-
stackoverflow.com/a/41365553/4432830 希望对您有所帮助
-
你的问题不清楚。您的意思是,如果您从 Main Activity 转到 Map 活动,则调用 onMapReady,但如果您从其他活动返回到 map 活动,则不会调用 onMapReady?
-
是的,这正是我的意思
-
在下面查看我的答案
-
@Praveen,一点帮助都没有 - 我的课堂上已经有
implements OnMapReadyCallback
标签: java android google-maps-android-api-2