Android 裝載器—使用LoaderManager的回調方法

來源:互聯網
上載者:User

LoaderManager.LoaderCallbacks是一個讓客戶與LoadManager進行互動的回調介面。
裝載器,尤其是CursorLoader裝載器,被期望用來儲存被終止後的資料,這樣就允許應用程式在Activity之間或Fragment的onStop()和onStart()方法之間進行切換時儲存資料,以便在使用者返回應用程式時,它們不需要因資料重載而等待。使用LoaderManager.LoaderCallbacks()的回調方法就知道在什麼時候要建立一個新的裝載器,並且告訴應用程式什麼時候是終止使用裝載器資料的時間。

LoaderManager.LoaderCallbacks介面包含以下方法:

1. onCreateLoader()---用給定的ID執行個體化並返回一個新的Loader對象;

2. onLoaderFinished()---當之前建立的裝載器已經完成它的裝載時,調用這個方法;

3. onLoaderReset()---當之前建立的裝載器被重設時,調用這個方法,這樣會使這個裝載器的資料變的無效。

下面分別對這些方法進行詳細說明。

onCreateLoader

當你想要訪問一個裝載器是時(例如,通過initLoader()方法),它會檢查指定ID的裝載器是否存在,如果不存在,它會觸發LoaderManager.LoaderCallbacks的onCreateLoader()回調方法。這是建立一個新的裝載器的地方,典型的是建立一個CursorLoader裝載器,但是你能夠實現你自己的Loader類的子類。

例如:

 // If non-null, this is the current filter the user has provided.
String mCurFilter;
...
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // This is called when a new Loader needs to be created.  This
    // sample only has one Loader, so we don't care about the ID.
    // First, pick the base URI to use depending on whether we are
    // currently filtering.
    Uri baseUri;
    if (mCurFilter != null) {
        baseUri = Uri.withAppendedPath(Contacts.CONTENT_FILTER_URI,
                  Uri.encode(mCurFilter));
    } else {
        baseUri = Contacts.CONTENT_URI;
    }

    // Now create and return a CursorLoader that will take care of
    // creating a Cursor for the data being displayed.
    String select = "((" + Contacts.DISPLAY_NAME + " NOTNULL) AND ("
            + Contacts.HAS_PHONE_NUMBER + "=1) AND ("
            + Contacts.DISPLAY_NAME + " != '' ))";
    return new CursorLoader(getActivity(), baseUri,
            CONTACTS_SUMMARY_PROJECTION, select, null,
            Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}

在這個列子中,onCreateLoader()回調方法建立了一個CursorLoader裝載器,你必須使用構造器方法來建立這個CursorLoader對象,它需要一整套執行對ContentProvider的查詢所需要資訊,具體需要如下:

1.  uri---要擷取的內容的位置(URI);

2.  projection---一個要返回的列的列表,如果傳遞null,將返回所有的列,這樣是低效的。

3.  selection---一個要返回的行的過濾器聲明,它使用SQL WHERE子句的格式(不包括WHERE關鍵字本身)。傳遞null時將返回給定URI的所有行資料。

4.  selectionArgs---你可以在selection中包含“?”字元,這些字元將會被selectionArgs中的值順序替換。這些值將作為字串被綁定。

5.  sortOrder---使用SQL ORDER BY子句的格式(不包括ORDER BY自己)指定行記錄是如何排序的,如果傳遞null將會使用預設的排序,也可以是無序的。

onLoadFinished

當之前建立的裝載器已經完成了它的載入時這個方法會被調用。在提供給這個裝載器的最後的資料釋放之前會保證調用這個方法。在這個時點你應該刪除所有的對舊資料使用(因為資料即將被釋放),但是你不應該自己做資料的釋放,因為裝載器本身會做這件事情。

裝載器一旦知道應用程式不在使用它,就會釋放資料。例如,如果資料是來自CursorLoader對象的一個遊標,你不應該自己來調用close()方法,如果遊標被放到了CursorAdapter中,你應該使用swapCursor()方法,以便舊的Cursor對象不被關閉。如:

// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
...

public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    // Swap the new cursor in.  (The framework will take care of closing the
    // old cursor once we return.)
    mAdapter.swapCursor(data);
}

 

onLoaderReset

當之前被建立的裝載器被重設的時候,這個方法會被調用。這樣就是它的資料變的無效。這個回調能夠讓你找到資料被釋放的時機,以便你能夠在資料被釋放之前刪除對它的引用。

用帶有null參數的swapCursor()方法實現這樣的調用,如:

// This is the Adapter being used to display the list's data.
SimpleCursorAdapter mAdapter;
...

public void onLoaderReset(Loader<Cursor> loader) {
    // This is called when the last Cursor provided to onLoadFinished()
    // above is about to be closed.  We need to make sure we are no
    // longer using it.
    mAdapter.swapCursor(null);
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.