[英]Android - using database with onRetainNonConfigurationInstance
package com.commonsware.cwac.wakeful.demo;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.widget.SimpleCursorAdapter;
public class FlightListActivity extends ListActivity {
private SQLiteDatabase database;
private String fields[] = {BaseColumns._ID, "name", "flights", "distance"};
private SimpleCursorAdapter dataSource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(ACTIVITY_SERVICE, "onCreate flights");
database = (SQLiteDatabase) getLastNonConfigurationInstance();
if (database == null) {
database = getData();
Log.v(ACTIVITY_SERVICE, "first load data");
}
Cursor data = database.query("pilots", fields, null, null, null, null, null);
dataSource = new CustomCursorAdapter(this, R.layout.row, data, fields, new int[] { R.id.id, R.id.name, R.id.flights, R.id.distance });
setListAdapter(dataSource);
}
protected SQLiteDatabase getData() {
DataBaseHelper myDbHelper = new DataBaseHelper(this.getApplicationContext());
return myDbHelper.openDataBase();
}
@Override
public Object onRetainNonConfigurationInstance() {
Log.v(ACTIVITY_SERVICE, "reusing data");
final SQLiteDatabase myData = database;
return myData;
}
@Override
protected void onDestroy() {
database.close();
super.onDestroy();
}
}
Ok so I am trying to hold on to my database by storing it in the onRetainNonConfigurationInstance, but I get a runtime error if I do not close the database in the onDestroy method. 好的,所以我试图通过将数据库存储在onRetainNonConfigurationInstance上来保留数据库,但是如果不使用onDestroy方法关闭数据库,则会收到运行时错误。
What I don't get it, if I close the database in onDestroy, then I need to reopen it again somewhere, but doesn't that defeat the object of holding on to it in the first place? 我没有得到什么,如果我在onDestroy中关闭了数据库,那么我需要在某个地方重新打开它,但是那不是打败了坚持它的对象吗?
Is this the best way to reuse the database when user rotates the device? 这是用户旋转设备时重用数据库的最佳方法吗?
In general, don't worry about this stuff - Android as actually very efficient when it comes to doing this sort of thing. 总的来说,不必担心这些事情-Android在执行此类操作时实际上非常高效。 Everything has to be recreated from scratch so in most cases, just let it happen.
一切都必须从头开始重新创建,因此在大多数情况下,只要让它发生就可以。
Opening the DB has a minimal cost in comparison to a query on it which might return a huge number of results. 与对数据库的查询相比,打开数据库的成本最低,这可能会返回大量结果。 In saying that, that's the point of a
Cursor
in that it is designed to handle the results from a query in an efficient manner. 如此说来,这就是
Cursor
所在,它旨在以高效的方式处理查询的结果。
As for ListViews
they only ever have a finite number of 'items' at any one time which are recycled as the ListView
is scrolled and during an orientation change, the ListView
will need to be re-drawn usually with a different number of visible list items. 至于
ListViews
它们在任何一次都只有有限数量的“项目”,这些内容在滚动ListView
时会被回收,并且在方向更改期间,通常需要使用不同数量的可见列表项重新绘制ListView
。
The use of onRetainNonConfigurationInstance()
is a special case designed for more complex scenarioss - perhaps retain a canvas the user is drawing on, or 'live' network connections which are maintaining some sort of session state (authentication tokens etc). onRetainNonConfigurationInstance()
的使用是为更复杂的场景设计的一种特殊情况-可能保留用户正在绘制的画布,或者保持某些会话状态(身份验证令牌等)的“实时”网络连接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.