应用程序从其他活动返回后不恢复捆绑

时间:2015-03-16 16:16:24

标签: android android-intent bundle instance android-savedstate

我有一个使用谷歌地图的应用程序。我将latLngBounds保存到变量,然后捆绑以便以后检索它。我的问题是我想保存屏幕的当前位置,然后将相机移动到LatLng。但每次当我点击标记并启动另一个活动并返回时,屏幕都会被点击标记居中,但我想摆脱这个功能,让屏幕返回到之前的位置。

编辑:问题是我的应用程序没有从bundle恢复数据,我不知道为什么。以下是我保存和恢复状态的方法,但两者都不起作用:

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    //requestWindowFeature(Window.FEATURE_ACTION_BAR);
    setContentView(R.layout.activity_main);
    if (bundle!= null){
        lat = bundle.getDouble("lat1",0);
        lon = bundle.getDouble("lon1",0);
        ne3 = bundle.getDouble("lat2",-200);
        ne4 = bundle.getDouble("lon2",-200);
        Log.d("tag","bundle is not null");
    }
    setUpMapIfNeeded();
}
@Override
public void onPause() {
    super.onPause();
    manager.removeUpdates(this);
}

@Override
protected void onResume() {
    super.onResume();
    if (bundle!= null){
        lat = bundle.getDouble("lat1",0);
        lon = bundle.getDouble("lon1",0);
        ne3 = bundle.getDouble("lat2",-200);
        ne4 = bundle.getDouble("lon2",-200);
        Log.d("tag","bundle is not null");
    }
    setUpMapIfNeeded();
}
@Override
protected void onSaveInstanceState(Bundle bundle) {
    Log.e("tag","calling onSave");
    if(mMap!=null){
    LatLngBounds location3 = mMap.getProjection().getVisibleRegion().latLngBounds;
    double lat4 = location3.getCenter().latitude;
    double lon4 = location3.getCenter().longitude;
    double lat5 = location3.northeast.latitude;
    double lon5 = location3.northeast.longitude;
    bundle.putDouble("lat1", lat4);
    bundle.putDouble("lon1",lon4);
    bundle.putDouble("lat2",lat5);
    bundle.putDouble("lon2",lon5);
    }
    super.onSaveInstanceState(bundle);
}
@Override
public void onRestoreInstanceState(Bundle bundle) {
    super.onRestoreInstanceState(bundle);
    if (bundle!= null){
        lat = bundle.getDouble("lat1",0);
        lon = bundle.getDouble("lon1",0);
        ne3 = bundle.getDouble("lat2",-200);
        ne4 = bundle.getDouble("lon2",-200);
        Log.d("tag","bundle is not null");
    }
    setUpMapIfNeeded();
    // ... recover more data
}

当从另一个活动

返回时,问题似乎是以某种方式为空

1 个答案:

答案 0 :(得分:0)

问题是地图在启动onSavedInstanceState()之前自动中心了,所以我保存了居中的数据,这就是它无法正常工作的原因。