【问题标题】:App not restoring bundle after returning from another activity从另一个活动返回后应用程序未恢复捆绑包
【发布时间】:2015-05-18 20:35:07
【问题描述】:

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

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

@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
}

问题似乎是从另一个活动返回时捆绑包以某种方式为空

【问题讨论】:

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


    【解决方案1】:

    问题是地图在启动 onSavedInstanceState() 之前自动居中,所以我保存了居中的数据,这就是它不起作用的原因。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多