简体   繁体   English

Android-将数据库与onRetainNonConfigurationInstance一起使用

[英]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.

相关问题 Android:onRetainNonConfigurationInstance()已弃用? - Android: onRetainNonConfigurationInstance() deprecated? Android-一种触发onRetainNonConfigurationInstance的方法? - Android - A way to trigger onRetainNonConfigurationInstance? 使用 onRetainNonConfigurationInstance 的替代方法 - Alternatives to using onRetainNonConfigurationInstance 使用onRetainNonConfigurationInstance()时TextViews消失 - TextViews disappear when using onRetainNonConfigurationInstance() 如何在Android的onRetainNonConfigurationInstance()中保存大量对象 - How to save lot of object in onRetainNonConfigurationInstance() in android Android:屏幕方向更改时未调用onRetainNonConfigurationInstance - Android : onRetainNonConfigurationInstance is not called on Screen orientation change Android:onRetainNonConfigurationInstance()和方向随着不同的布局而改变 - Android: onRetainNonConfigurationInstance() and orientation changes with different layouts 维护Android Activity的数据:onPause,onSaveInstanceState,onRetainNonConfigurationInstance - Maintaining Android Activity's data: onPause, onSaveInstanceState, onRetainNonConfigurationInstance 从onRetainNonConfigurationInstance()获取值时出现空指针异常-Android - Null pointer exception on getting value from onRetainNonConfigurationInstance() - Android Android:我可以通过 onRetainNonConfigurationInstance 保存一个 Drawable 或 Bitmap - Android: can I save a Drawable or Bitmap via onRetainNonConfigurationInstance
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM