从一个HOME键preSS回来后onLoadFinished不叫不叫、回来后、HOME、preSS

2023-09-05 06:49:12 作者:褪色灵感

在使用自定义的 AsyncTaskLoader 来从Web服务下载数据,如果我preSS的主页按钮加载过程中间,然后输入应用程序再次,在onLoadFinished()方法将不会调用。在 onActivityCreated我的片段叫 setRetainInstance(真)(),它也要求 getLoaderManager。 initLoader(0,空,这一点)中相同的方法(如推荐)。

When using a custom AsyncTaskLoader to download data from a web service, if I press the HOME button in the middle of the loading process and then enter the app again, the onLoadFinished() method is not called. My fragment is calling setRetainInstance(true) in onActivityCreated() and it also calls getLoaderManager.initLoader(0, null, this) in the same method (as is recommended).

测试时,我看到,快到的时候回片段 onActivityCreated()不叫所以这可能是为什么 onLoadFinished()不叫。但是,还有什么地方可以把 initLoader()的方法?我读过的几个地方,它不应该被称为 onResume()

While testing, I see that when coming back to the fragment onActivityCreated() is not called so this may be why onLoadFinished() is not called. But where else to put the initLoader() method? I've read in several places that it should not be called in onResume().

所以,任何想法?我有很多在我的应用程序的各种屏幕装载机,我需要在一个优雅的方式来解决这个问题。

So, any ideas? I have a lot of loaders in various screens in my app and I need to solve this issue in an elegant way.

推荐答案

在看问题14944(http://$c$c.google.com/p/android/issues/detail?id=14944),我通过覆盖解决的问题 onStartLoading()在我的自定义 AsyncTaskLoader 和呼叫的forceload()

After looking at Issue 14944 (http://code.google.com/p/android/issues/detail?id=14944), I solved the issue by overriding onStartLoading() in my custom AsyncTaskLoader and call forceLoad().

这是更好的解决方案是创建一个自定义父 AsyncTaskLoader ,看起来像这样(从建议采取alexvem从上面的链接):

An even better solution is to create a custom parent AsyncTaskLoader that looks like this (taken from a suggestion by alexvem from the link above):

public abstract class AsyncLoader<D> extends AsyncTaskLoader<D> {

    private D data;

    public AsyncLoader(Context context) {
        super(context);
    }

    @Override
    public void deliverResult(D data) {
        if (isReset()) {
            // An async query came in while the loader is stopped
            return;
        }

        this.data = data;

        super.deliverResult(data);
    }


    @Override
    protected void onStartLoading() {
        if (data != null) {
            deliverResult(data);
        }

        if (takeContentChanged() || data == null) {
            forceLoad();
        }
    }

    @Override
    protected void onStopLoading() {
         // Attempt to cancel the current load task if possible.
        cancelLoad();
    }

    @Override
    protected void onReset() {
        super.onReset();

        // Ensure the loader is stopped
        onStopLoading();

        data = null;
    }
}
 
精彩推荐
图片推荐