如何在关闭应用程序时恢复用户的以前的数据并显示以前保存的实例?
我正在研究GoogleMaps,并且我在google地图上放置了标记。标记信息我通过调用API获取。如何在关闭应用程序时恢复用户的以前的数据并显示以前保存的实例?
现在我的问题是,如果用户没有互联网连接,或者如果它关闭,我不希望该应用程序应该崩溃,而应该显示用户上次会话标记放置在谷歌地图上。
我已经阅读并尝试了一些使用onPause()或onStart()方法可以完成的方法。但我没有得到它:
源代码看起来像这样。
public class MapActivity extends Activity{
// onCreate
// making an asynchronous call and storing the latitude and longitude of multiple marker in varibale place
doInBAckground(){
place = apicall.getPlaces();
}
onPostExecute(){
plotmarkers(place);
}
private void plotMarkers(List<Place> markers) {
if (markers.size() > 0) {
for (Place myMarker : markers) {
MarkerOptions markerOption = new MarkerOptions().position(new LatLng(myMarker.getLatitude(), myMarker.getLongitude()));
markerOption.icon(BitmapDescriptorFactory.fromResource(markerImage));
Marker currentMarker = mMap.addMarker(markerOption);
mMarkersHashMap.put(currentMarker, myMarker);
}
}
}
}
现在我想要的是我应该能够显示用户以前保存的实例。 我没有得到这个工作..我见过OLA出租车应用程序能够做到这一点。
请让我知道如何处理它... 感谢名单提前.. 如果需要更多的片段只是让我知道
,你可以:
- 让你的HTTP客户端缓存它适合你
- 保存数据以及活动状态
- 坚持下载的数据到硬盘
你选择哪一个去与取决于你多大的控制要在高速缓存中。
HTTP客户端缓存 缓存很容易实现。您只需将您的HTTP客户端配置为使用磁盘缓存。使用OkHTTP那看起来会是这样的:
private static final int DISK_CACHE_SIZE = 2 << 20; // 2MB cache
OkHttpClient createOkHttpClient(Application app) {
OkHttpClient client = new OkHttpClient();
// Install an HTTP cache in the application cache directory.
try {
File cacheDir = new File(app.getCacheDir(), "http");
Cache cache = new Cache(cacheDir, DISK_CACHE_SIZE);
client.setCache(cache);
} catch (IOException e) {
LOG.e("<tag>", "Unable to install disk cache.", e);
}
return client;
}
与活动状态保存 我不推荐这种方法。它只适用于您的在线数据变化如此之快以至于长时间持续不会对用户有帮助的情况。在这种情况下,您可以将它与活动状态一起保存,因为它在那里只持续很短的时间。阅读关于实施这个here。
坚持硬盘 这是我的首选解决方案。您可以以任何您喜欢的方式保存数据,然后再次加载。你可以使用共享首选项,或使用SQLite。如果您选择使用SQLite,则应该查找使实现更容易的库。我知道Cupboard和OrmLite都不错。
您可以覆盖onSaveInstanceState()方法。
这是你如何保存你的数据:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
// save marker data
savedInstanceState.putString("coordinates", mMarker.getCoordinates());
// always call the superclass so it can save the view hierarchy state
super.onSaveInstanceState(savedInstanceState);
}
注:后来,我建议实施在 您的机型(如地点,标记,的MarkerOptions)Parcelable接口,让您轻松 放你的整个物体在捆绑中。它比 处理单个成员并为它们的每个 创建单独的密钥更方便。
这是你如何找回你的onCreate()方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// check whether we're recreating a previously destroyed instance
if (savedInstanceState != null) {
// restore value from saved state
String coordinates = savedInstanceState.getString("coordinates");
// do something with the restored data
} else {
// download from API
// do something with the downloaded data
}
...
}
欲了解更多信息,请查看the official documentation
我试过这种方法,但它总是给我onSaveInstance为空.. – anand 2014-11-03 08:57:00
而其他的答案提供了一个很好的方式来保存标记,你也应该牢记在Google Maps terms of use列出的规则:
“内容”指通过服务提供的任何内容(无论 通过创建谷歌或其第三方许可人),包括地图数据,摄影图像,交通数据,地点数据 (包括商业列表)或任何其他内容。
你不能预先撷取高速缓存,或储存任何内容,除非你 可以存储:(一)数量有限的内容以提高 如果你做你的地图API实现的性能的目的所以 暂时(且在没有事件超过30个历日内) 牢固,并且在不允许使用内容 服务以外的方式;
确定我将与SharedPreference方法尝试...... – anand 2014-11-03 08:49:40
感谢我得到了我与Sharedpefernce解决方案... – anand 2014-11-03 10:03:58