Example usage for android.app LoaderManager restartLoader

List of usage examples for android.app LoaderManager restartLoader

Introduction

In this page you can find the example usage for android.app LoaderManager restartLoader.

Prototype

public abstract <D> Loader<D> restartLoader(int id, Bundle args, LoaderManager.LoaderCallbacks<D> callback);

Source Link

Document

Starts a new or restarts an existing android.content.Loader in this manager, registers the callbacks to it, and (if the activity/fragment is currently started) starts loading it.

Usage

From source file:com.google.samples.apps.iosched.ui.SessionDetailFragment.java

private void onFeedbackQueryComplete(Cursor cursor) {
    // Views have not been set up yet -- continue loading the rest of data
    if (mSubmitFeedbackView == null) {
        LoaderManager manager = getLoaderManager();
        manager.restartLoader(SessionsQuery._TOKEN, null, this);
        manager.restartLoader(SpeakersQuery._TOKEN, null, this);
        manager.restartLoader(TAG_METADATA_TOKEN, null, this);
    }//from   www .ja  v  a2s  . co  m

    // Is there existing feedback for this session?
    mAlreadyGaveFeedback = cursor.getCount() > 0;

    if (mAlreadyGaveFeedback) {
        final MessageCardView giveFeedbackCardView = (MessageCardView) mRootView
                .findViewById(R.id.give_feedback_card);
        if (giveFeedbackCardView != null) {
            giveFeedbackCardView.setVisibility(View.GONE);
        }
        if (mSubmitFeedbackView != null) {
            mSubmitFeedbackView.setVisibility(View.GONE);
        }
    }
    LOGD(TAG, "User " + (mAlreadyGaveFeedback ? "already gave" : "has not given") + " feedback for session.");
    cursor.close();
}

From source file:com.android.browser.BookmarksPageCallbacks.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if (loader.getId() == LOADER_ACCOUNTS) {
        LoaderManager lm = getLoaderManager();
        int id = LOADER_BOOKMARKS;
        while (cursor.moveToNext()) {
            String accountName = cursor.getString(0);
            String accountType = cursor.getString(1);
            Bundle args = new Bundle();
            args.putString(ACCOUNT_NAME, accountName);
            args.putString(ACCOUNT_TYPE, accountType);
            BrowserBookmarksAdapter adapter = new BrowserBookmarksAdapter(getActivity());
            mBookmarkAdapters.put(id, adapter);
            boolean expand = true;
            try {
                expand = mState.getBoolean(
                        accountName != null ? accountName : BookmarkExpandableView.LOCAL_ACCOUNT_NAME);
            } catch (JSONException e) {
            } // no state for accountName
            mGrid.addAccount(accountName, adapter, expand);
            lm.restartLoader(id, args, this);
            id++;/*from  ww w  .  j av  a 2  s.  c  om*/
        }
        // TODO: Figure out what a reload of these means
        // Currently, a reload is triggered whenever bookmarks change
        // This is less than ideal
        // It also causes UI flickering as a new adapter is created
        // instead of re-using an existing one when the account_name is the
        // same.
        // For now, this is a one-shot load
        getLoaderManager().destroyLoader(LOADER_ACCOUNTS);
    } else if (loader.getId() >= LOADER_BOOKMARKS) {
        BrowserBookmarksAdapter adapter = mBookmarkAdapters.get(loader.getId());
        adapter.changeCursor(cursor);
    }
}

From source file:com.android.xbrowser.BookmarksPageCallbacks.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if (loader.getId() == LOADER_ACCOUNTS) {
        LoaderManager lm = getLoaderManager();
        int id = LOADER_BOOKMARKS;
        while (cursor.moveToNext()) {
            String accountName = cursor.getString(0);
            String accountType = cursor.getString(1);
            Bundle args = new Bundle();
            args.putString(ACCOUNT_NAME, accountName);
            args.putString(ACCOUNT_TYPE, accountType);
            BrowserBookmarksAdapter adapter = new BrowserBookmarksAdapter(getActivity(), VIEW_THUMBNAILS);
            mBookmarkAdapters.put(id, adapter);
            boolean expand = true;
            try {
                expand = mState.getBoolean(
                        accountName != null ? accountName : BookmarkExpandableView.LOCAL_ACCOUNT_NAME);
            } catch (JSONException e) {
            } // no state for accountName
            mGrid.addAccount(accountName, adapter, expand);
            lm.restartLoader(id, args, this);
            id++;/*from  w ww. jav a  2  s .  c  om*/
        }
        // TODO: Figure out what a reload of these means
        // Currently, a reload is triggered whenever bookmarks change
        // This is less than ideal
        // It also causes UI flickering as a new adapter is created
        // instead of re-using an existing one when the account_name is the
        // same.
        // For now, this is a one-shot load
        getLoaderManager().destroyLoader(LOADER_ACCOUNTS);
    } else if (loader.getId() >= LOADER_BOOKMARKS) {
        BrowserBookmarksAdapter adapter = mBookmarkAdapters.get(loader.getId());
        adapter.changeCursor(cursor);
    }
}

From source file:com.borqs.browser.combo.BookmarksPageCallbacks.java

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
    if (loader.getId() == LOADER_ACCOUNTS) {
        LoaderManager lm = getLoaderManager();
        int id = LOADER_BOOKMARKS;
        while (cursor.moveToNext()) {
            String accountName = cursor.getString(0);
            String accountType = cursor.getString(1);
            Bundle args = new Bundle();
            args.putString(ACCOUNT_NAME, accountName);
            args.putString(ACCOUNT_TYPE, accountType);
            BrowserBookmarksAdapter adapter = new BrowserBookmarksAdapter(this);
            mBookmarkAdapters.put(id, adapter);
            boolean expand = true;
            try {
                expand = mState.getBoolean(
                        accountName != null ? accountName : BookmarkExpandableView.LOCAL_ACCOUNT_NAME);
            } catch (JSONException e) {
            } // no state for accountName
            mGrid.addAccount(accountName, adapter, expand);
            lm.restartLoader(id, args, this);
            id++;/*from   w w w . j a v  a  2 s.  c o m*/
        }
        // TODO: Figure out what a reload of these means
        // Currently, a reload is triggered whenever bookmarks change
        // This is less than ideal
        // It also causes UI flickering as a new adapter is created
        // instead of re-using an existing one when the account_name is the
        // same.
        // For now, this is a one-shot load
        getLoaderManager().destroyLoader(LOADER_ACCOUNTS);
    } else if (loader.getId() >= LOADER_BOOKMARKS) {
        BrowserBookmarksAdapter adapter = mBookmarkAdapters.get(loader.getId());
        adapter.changeCursor(cursor);
    }
}

From source file:com.tct.mail.ui.FolderListFragment.java

/**
 * Sets the current account to the one provided here.
 * @param account the current account to set to.
 *///  w w  w. ja  va2 s  .c o m
private void setSelectedAccount(Account account) {
    final boolean changed = (account != null)
            && (mCurrentAccount == null || !mCurrentAccount.uri.equals(account.uri));
    mCurrentAccount = account;
    if (changed) {
        // Verify that the new account supports sending application feedback
        updateFooterItems();
        // We no longer have proper folder objects. Let the new ones come in
        mFolderAdapter.setCursor(null);
        // If currentAccount is different from the one we set, restart the loader. Look at the
        // comment on {@link AbstractActivityController#restartOptionalLoader} to see why we
        // don't just do restartLoader.
        final LoaderManager manager = getLoaderManager();
        manager.destroyLoader(FOLDER_LIST_LOADER_ID);
        manager.restartLoader(FOLDER_LIST_LOADER_ID, Bundle.EMPTY, this);
        manager.destroyLoader(ALL_FOLDER_LIST_LOADER_ID);
        manager.restartLoader(ALL_FOLDER_LIST_LOADER_ID, Bundle.EMPTY, this);
        // An updated cursor causes the entire list to refresh. No need to refresh the list.
        // But we do need to blank out the current folder, since the account might not be
        // synced.
        mSelectedFolderUri = FolderUri.EMPTY;
        mCurrentFolderForUnreadCheck = null;

        // also set/update the mini-drawer
        if (mMiniDrawerEnabled) {
            //foobar
            mMiniDrawerView.refresh();
        }

    } else if (account == null) {
        // This should never happen currently, but is a safeguard against a very incorrect
        // non-null account -> null account transition.
        LogUtils.e(LOG_TAG, "FLF.setSelectedAccount(null) called! Destroying existing loader.");
        final LoaderManager manager = getLoaderManager();
        manager.destroyLoader(FOLDER_LIST_LOADER_ID);
        manager.destroyLoader(ALL_FOLDER_LIST_LOADER_ID);
    }
}

From source file:com.android.mail.ui.FolderListFragment.java

/**
 * Sets the current account to the one provided here.
 * @param account the current account to set to.
 */// w  w  w  .j a  v a 2 s  .c om
private void setSelectedAccount(Account account) {
    final boolean changed = (account != null)
            && (mCurrentAccount == null || !mCurrentAccount.uri.equals(account.uri));
    mCurrentAccount = account;
    if (changed) {
        // Verify that the new account supports sending application feedback
        updateFooterItems();
        // We no longer have proper folder objects. Let the new ones come in
        mFolderAdapter.setCursor(null);
        // If currentAccount is different from the one we set, restart the loader. Look at the
        // comment on {@link AbstractActivityController#restartOptionalLoader} to see why we
        // don't just do restartLoader.
        final LoaderManager manager = getLoaderManager();
        manager.destroyLoader(FOLDER_LIST_LOADER_ID);
        manager.restartLoader(FOLDER_LIST_LOADER_ID, Bundle.EMPTY, this);
        manager.destroyLoader(ALL_FOLDER_LIST_LOADER_ID);
        manager.restartLoader(ALL_FOLDER_LIST_LOADER_ID, Bundle.EMPTY, this);
        // An updated cursor causes the entire list to refresh. No need to refresh the list.
        // But we do need to blank out the current folder, since the account might not be
        // synced.
        mSelectedFolderUri = FolderUri.EMPTY;
        mCurrentFolderForUnreadCheck = null;

        // also set/update the mini-drawer
        if (mMiniDrawerAccountsAdapter != null) {
            mMiniDrawerAccountsAdapter.setAccounts(getAllAccounts(), mCurrentAccount);
        }

    } else if (account == null) {
        // This should never happen currently, but is a safeguard against a very incorrect
        // non-null account -> null account transition.
        LogUtils.e(LOG_TAG, "FLF.setSelectedAccount(null) called! Destroying existing loader.");
        final LoaderManager manager = getLoaderManager();
        manager.destroyLoader(FOLDER_LIST_LOADER_ID);
        manager.destroyLoader(ALL_FOLDER_LIST_LOADER_ID);
    }
}

From source file:com.android.mail.ui.AbstractActivityController.java

/**
 * {@link LoaderManager} currently has a bug in
 * {@link LoaderManager#restartLoader(int, Bundle, android.app.LoaderManager.LoaderCallbacks)}
 * where, if a previous onCreateLoader returned a null loader, this method will NPE. Work around
 * this bug by destroying any loaders that may have been created as null (essentially because
 * they are optional loads, and may not apply to a particular account).
 * <p>//from   w  ww  . j av a  2 s.co m
 * A simple null check before restarting a loader will not work, because that would not
 * give the controller a chance to invalidate UI corresponding the prior loader result.
 *
 * @param id loader ID to safely restart
 * @param handler the LoaderCallback which will handle this loader ID.
 * @param args arguments, if any, to be passed to the loader. Use {@link Bundle#EMPTY} if no
 *             arguments need to be specified.
 */
private void restartOptionalLoader(int id, LoaderManager.LoaderCallbacks handler, Bundle args) {
    final LoaderManager lm = mActivity.getLoaderManager();
    lm.destroyLoader(id);
    lm.restartLoader(id, args, handler);
}

From source file:com.chen.mail.ui.AbstractActivityController.java

/**
 * Sets the current folder if it is different from the object provided here. This method does
 * NOT notify the folder observers that a change has happened. Observers are notified when we
 * get an updated folder from the loaders, which will happen as a consequence of this method
 * (since this method starts/restarts the loaders).
 * @param folder The folder to assign//from   w  w  w  .j  a v  a  2 s  . co m
 */
private void updateFolder(Folder folder) {
    if (folder == null || !folder.isInitialized()) {
        LogUtils.e(LOG_TAG, new Error(), "AAC.setFolder(%s): Bad input", folder);
        return;
    }
    if (folder.equals(mFolder)) {
        LogUtils.d(LOG_TAG, "AAC.setFolder(%s): Input matches mFolder", folder);
        return;
    }
    final boolean wasNull = mFolder == null;
    LogUtils.d(LOG_TAG, "AbstractActivityController.setFolder(%s)", folder.name);
    final LoaderManager lm = mActivity.getLoaderManager();
    // updateFolder is called from AAC.onLoadFinished() on folder changes.  We need to
    // ensure that the folder is different from the previous folder before marking the
    // folder changed.
    setHasFolderChanged(folder);
    mFolder = folder;

    // We do not need to notify folder observers yet. Instead we start the loaders and
    // when the load finishes, we will get an updated folder. Then, we notify the
    // folderObservers in onLoadFinished.
    mActionBarView.setFolder(mFolder);

    // Only when we switch from one folder to another do we want to restart the
    // folder and conversation list loaders (to trigger onCreateLoader).
    // The first time this runs when the activity is [re-]initialized, we want to re-use the
    // previous loader's instance and data upon configuration change (e.g. rotation).
    // If there was not already an instance of the loader, init it.
    if (lm.getLoader(LOADER_FOLDER_CURSOR) == null) {
        lm.initLoader(LOADER_FOLDER_CURSOR, Bundle.EMPTY, mFolderCallbacks);
    } else {
        lm.restartLoader(LOADER_FOLDER_CURSOR, Bundle.EMPTY, mFolderCallbacks);
    }
    if (!wasNull && lm.getLoader(LOADER_CONVERSATION_LIST) != null) {
        // If there was an existing folder AND we have changed
        // folders, we want to restart the loader to get the information
        // for the newly selected folder
        lm.destroyLoader(LOADER_CONVERSATION_LIST);
    }
    final Bundle args = new Bundle(2);
    args.putParcelable(BUNDLE_ACCOUNT_KEY, mAccount);
    args.putParcelable(BUNDLE_FOLDER_KEY, mFolder);
    lm.initLoader(LOADER_CONVERSATION_LIST, args, mListCursorCallbacks);
}

From source file:com.android.mail.ui.AbstractActivityController.java

/**
 * Sets the current folder if it is different from the object provided here. This method does
 * NOT notify the folder observers that a change has happened. Observers are notified when we
 * get an updated folder from the loaders, which will happen as a consequence of this method
 * (since this method starts/restarts the loaders).
 * @param folder The folder to assign/*from   w w w .j a va2s.  com*/
 */
private void updateFolder(Folder folder) {
    if (folder == null || !folder.isInitialized()) {
        LogUtils.e(LOG_TAG, new Error(), "AAC.setFolder(%s): Bad input", folder);
        return;
    }
    if (folder.equals(mFolder)) {
        LogUtils.d(LOG_TAG, "AAC.setFolder(%s): Input matches mFolder", folder);
        return;
    }
    final boolean wasNull = mFolder == null;
    LogUtils.d(LOG_TAG, "AbstractActivityController.setFolder(%s)", folder.name);
    final LoaderManager lm = mActivity.getLoaderManager();
    // updateFolder is called from AAC.onLoadFinished() on folder changes.  We need to
    // ensure that the folder is different from the previous folder before marking the
    // folder changed.
    setHasFolderChanged(folder);
    mFolder = folder;

    // We do not need to notify folder observers yet. Instead we start the loaders and
    // when the load finishes, we will get an updated folder. Then, we notify the
    // folderObservers in onLoadFinished.
    mActionBarController.setFolder(mFolder);

    // Only when we switch from one folder to another do we want to restart the
    // folder and conversation list loaders (to trigger onCreateLoader).
    // The first time this runs when the activity is [re-]initialized, we want to re-use the
    // previous loader's instance and data upon configuration change (e.g. rotation).
    // If there was not already an instance of the loader, init it.
    if (lm.getLoader(LOADER_FOLDER_CURSOR) == null) {
        lm.initLoader(LOADER_FOLDER_CURSOR, Bundle.EMPTY, mFolderCallbacks);
    } else {
        lm.restartLoader(LOADER_FOLDER_CURSOR, Bundle.EMPTY, mFolderCallbacks);
    }
    if (!wasNull && lm.getLoader(LOADER_CONVERSATION_LIST) != null) {
        // If there was an existing folder AND we have changed
        // folders, we want to restart the loader to get the information
        // for the newly selected folder
        lm.destroyLoader(LOADER_CONVERSATION_LIST);
    }
    final Bundle args = new Bundle(2);
    args.putParcelable(BUNDLE_ACCOUNT_KEY, mAccount);
    args.putParcelable(BUNDLE_FOLDER_KEY, mFolder);
    args.putBoolean(BUNDLE_IGNORE_INITIAL_CONVERSATION_LIMIT_KEY, mIgnoreInitialConversationLimit);
    mIgnoreInitialConversationLimit = false;
    lm.initLoader(LOADER_CONVERSATION_LIST, args, mListCursorCallbacks);
}

From source file:com.tct.mail.ui.AbstractActivityController.java

/**
 * TCT: Move data load code to an independent function, if we just want refresh loader data.
 * call this to refresh cursor data(local search or update folder)
 */// w w  w.j  a va2  s.  c  om
private void loadConversationListData(boolean folderUpdated) {
    if (mFolder == null || !mFolder.isInitialized()) {
        LogUtils.e(LOG_TAG, new Error(), "AAC.setFolder(%s): Bad input", mFolder);
        return;
    }
    final LoaderManager lm = mActivity.getLoaderManager();

    if (mConvListContext != null && mConvListContext.isLocalSearchExecuted()) {
        final Bundle args = new Bundle(4);
        args.putParcelable(BUNDLE_ACCOUNT_KEY, mAccount);
        args.putParcelable(BUNDLE_FOLDER_KEY, mFolder);
        args.putString(SearchParams.BUNDLE_QUERY_FIELD, mConvListContext.getSearchField());
        args.putString(SearchParams.BUNDLE_QUERY_TERM, mConvListContext.getSearchQuery());
        LogUtils.logFeature(LogTag.SEARCH_TAG,
                "loadConversationListData for local search query [%s], field [%s]",
                mConvListContext.getSearchQuery(), mConvListContext.getSearchField());
        ///TCT: Before we start the local search loader, need cancel the normal conversation list
        // loader to avoid no need load and wrong result display by the load finish delay.@{
        if (lm.getLoader(LOADER_CONVERSATION_LIST) != null) {
            lm.destroyLoader(LOADER_CONVERSATION_LIST);
        }
        // @}

        lm.restartLoader(LOADER_LOCALSEARCH_CONVERSATION_LIST, args, mListCursorCallbacks);
    } else {

        ///TCT: Before we start the normal search loader, need cancel the local search conversation list
        // loader to avoid no need load and wrong result display by the load finish delay.@{
        if (lm.getLoader(LOADER_LOCALSEARCH_CONVERSATION_LIST) != null) {

            lm.destroyLoader(LOADER_LOCALSEARCH_CONVERSATION_LIST);

        }
        // @}

        // TS: kaifeng.lu 2015-09-8 EMAIL BUGFIX-1065353 DEL_S
        //            final ConversationCursorLoader ccl = (ConversationCursorLoader) ((Object) lm
        //                    .getLoader(LOADER_CONVERSATION_LIST));

        //            if (ccl != null && !ccl.getUri().equals(mFolder.conversationListUri) && folderUpdated) {

        // If there was an existing folder AND we have changed
        // folders, we want to restart the loader to get the information
        // for the newly selected folder
        //                lm.destroyLoader(LOADER_CONVERSATION_LIST);

        //            }
        // TS: kaifeng.lu 2015-09-8 EMAIL BUGFIX-1065353 DEL_E

        final Bundle args = new Bundle(2);
        args.putParcelable(BUNDLE_ACCOUNT_KEY, mAccount);
        args.putParcelable(BUNDLE_FOLDER_KEY, mFolder);
        args.putBoolean(BUNDLE_IGNORE_INITIAL_CONVERSATION_LIMIT_KEY, mIgnoreInitialConversationLimit);
        mIgnoreInitialConversationLimit = false;
        lm.initLoader(LOADER_CONVERSATION_LIST, args, mListCursorCallbacks);
    }
}