搜尋此網誌

2015年1月5日 星期一

【Android】When should close SQLiteDatabase

ref:
http://www.devlog.en.alt-area.org/?p=1019
http://stackoverflow.com/questions/18595482/do-i-need-to-call-getwritabledatabase-everytime-i-manipulate-data

You should close every cursor you open. But the database instance doesn't need to be closed after each use.
I usually fetch a writable database in onCreate() and store the resulting SQLiteDatabase as a member variable for each activity, and never explicitly close that instance. (But again, I do close every cursor as soon as I'm finished processing their results.)
See Managing SQLite Connections in Android for a little more discussion on the subject.

--------------------------------devide line----------------------------------------------------------------------
「SQLiteOpenHelper」 may return the same 「SQLiteDatabase」 object. Based on this, I will describe how to execute close() properly.

「SQLiteOpenHelper」 returns the same 「SQLiteDatabase」 object

Before, in my article You should close SQLiteDatabase in Android developing (1) | DeVlog, I said you should close 「SQLiteDatabase」 object properly.
Then, I found that the objects obtained from 「SQLiteOpenHelper」 may be the same 「SQLiteDatabase」 object. That is, 「SQLiteOpenHelper」 object holds mDatabasefield which is the 「SQLiteDatabase」 object and returns it.

You can be addressed with the same idea

However, the way of action is almost same to [Pattern 1] – [pattern 3] of the previous article. You shoud close properly.
[Pattern 1]
Get one 「SQLiteDatabase」 object from one 「SQLiteOpenHelper」 object when you need it, and close it when you no longer need it. I take this style basically.
This is as a previous article.
By the way, even if you close a 「SQLiteDatabase」 object that is obtained from a SQLiteHelper object and then you get a 「SQLiteDatabase」 object from the same Helper opbject, there is no problem. In that case, new 「SQLiteDatabase」 object will be created.
[Pattern 2]
If you retrieve multiple objects from a single 「SQLiteOpenHelper」 in one thread, you should call close() at once when no longer needed. To do so, use 「SQLiteOpenHelper#close()」. It will close the 「SQLiteDatabase」 object inside.
[Pattern 3]
If you want to use 「SQLiteDatabase」 object obtained from a 「SQLiteOpenHelper」 object in multi threads, you should use the 「aquireReference()」.
db1 and db2 in [Pattern2] or mDb1 and mDb2 in [Pattern3] looks different each other, but are the same entity. Therefore, their handling is the same as when dealing with multiple references in the previous article.
[Reference Sites]
  1. SQLiteOpenHelper | Android Developers
  2. SQLiteDatabase | Android Developers

2014年12月31日 星期三

【Android】控制手機上的"back"按鍵

ref:StackOverflow

using  for API level 5
@Override
public void onBackPressed() {
    // your code.
}
and for older then API 5 use this:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        // your code
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

2014年12月30日 星期二

【Android】onActivityResult的用法

ref:http://blog.csdn.net/sjf0115/article/details/7387467


主要功能:
在一個主界面(主Activity)上能連接往許多不同子功能模塊(子Activity上去),當子模塊的事情做完之後就回到主界面,或許還同時返回一些子模塊完成的數據交給主Activity處理。這樣的數據交流就要用到回調函數onActivityResult。


  • startActivityForResult(Intent intent, int requestCode);

第一個參數:一個Intent對象
第二個參數:如果> = 0,當Activity結束時requestCode將歸還在onActivityResult()中。以便確定返回的數據是從哪個Activity中返回


  • onActivityResult(int requestCode, int resultCode, Intent data)

第一個參數:這個整數requestCode提供給onActivityResult,是以便確認返回的數據是從哪個Activity返回的。
            這個requestCode和startActivityForResult中的requestCode相對應。
第二個參數:這整數resultCode是由子Activity通過其setResult()方法返回。
第三個參數:一個Intent對象,帶有返回的數據。


  • setResult(int resultCode, Intent data)

調用這個方法把Activity想要返回的數據返回到父Activity
第一個參數:當Activity結束時resultCode將歸還在onActivityResult()中,一般為RESULT_CANCELED , RESULT_OK。
第二個參數:一個Intent對象,返回給父Activity的數據。