活动重启时Asynctaskloader重新加载 [英] Asynctaskloader restart loading when the activity restart

查看:260
本文介绍了活动重启时Asynctaskloader重新加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个自定义asynctaskloader,以便在后台加载数据.问题是,当用户最小化应用程序(或从画廊中挑选照片)后返回到活动时,与活动相关联的所有asynctaskloaders和与活动中的片段相关联的asynctaskloaders都重新开始.

Hi I'm implementing a custom asynctaskloader to load data in the background. The problem is when the user goes back to the activity after he minimizes the app(or pick photo from gallary), all the asynctaskloaders associated with the activity and asynctaskloaders associated with the fragments in the activity start all over again.

当活动重新启动时,如何防止加载器重新启动加载过程?

How can I prevent the loader to restart the loading process when the activity restarts?

我的baseAsynctaskLoader类:

My baseAsynctaskLoader class:

import android.content.Context;

导入android.support.v4.content.AsyncTaskLoader; 导入android.util.Log;

import android.support.v4.content.AsyncTaskLoader; import android.util.Log;

公共抽象类BaseAsyncTaskLoader扩展了AsyncTaskLoader {

public abstract class BaseAsyncTaskLoader extends AsyncTaskLoader{

private static final String TAG = "BaseAsyncTaskLoader";

protected Context context;
protected Object mData;

public BaseAsyncTaskLoader( Context context ) {
    super(context);
    Log.d(TAG, "BaseLoader");
    this.context = context.getApplicationContext();
}

@Override
public abstract Object loadInBackground();

@Override
public void deliverResult(Object data){
    Log.d( TAG, "deliverResult" );
    if (isReset()) {
        return;
    }
    mData = data;

    if (isStarted()) {
        super.deliverResult(data);
    }

}

@Override
protected void onStartLoading(){
    Log.d( TAG, "onStartLoading" );

    if (mData != null) {
        deliverResult(mData);
    }

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

@Override
protected void onStopLoading(){
    Log.d( TAG, "onStopLoading" );
    cancelLoad();
}

@Override
protected void onReset(){
    Log.d( TAG, "onReset" );
    super.onReset();
    onStopLoading();
    if(mData !=null){
        mData=null;
    }
}

@Override
public void onCanceled(Object data){
    Log.d( TAG, "onCanceled" );
    super.onCanceled(data);
}

}

Loader类示例:

Loader class sample:

public class AddDetailService extends BaseAsyncTaskLoader{

Activity activity;
Bundle bundle;

String outputstringrespone="";

public AddCarService(Activity activity, Bundle bundle){
    super(activity);
    this.activity = activity;
    this.bundle = bundle;
}
@Override
public Object loadInBackground(){
    try{

        int userId = bundle.getInt(USER_ID);
        int modelId = bundle.getInt(MODEL_ID);
        outputstringrespone = addDetails(userId, modelId);

    }catch(Exception e){
        Log.d(TAG, e.toString());
    }
    return null;
}

@Override
public void deliverResult(Object data){
    super.deliverResult(data);
    Log.d(TAG,"output--"+outputstringrespone);
    if(outputstringrespone.equalsIgnoreCase("Success")){
        Toast.makeText(activity, "Details Added Successfully", Toast.LENGTH_SHORT).show();

    }else {
        Toast.makeText(activity, "Details not added",Toast.LENGTH_SHORT).show();
    }

}

}

活动中的LoaderCallback:

LoaderCallback in activity:

getLoaderManager().initLoader(LoaderId.ADD_DETAIL,bundle,addDetailsCallbacks);

getLoaderManager().initLoader(LoaderId.ADD_DETAIL, bundle, addDetailsCallbacks);

LoaderManager.LoaderCallbacks addDetailsCallbacks = new LoaderManager.LoaderCallbacks(){
    @Override
    public Loader onCreateLoader(int id, Bundle args){
        return new AddDetailService(getActivity(), args);
    }

    @Override
    public void onLoadFinished(Loader loader, Object data){
        getLoaderManager().destroyLoader(LoaderId.ADD_DETAIL);
    }

    @Override
    public void onLoaderReset(Loader loader){

    }
};

推荐答案

这是LoaderManager框架的预期行为-如果包含ActivityFragment重新创建.

This is the desired behavior of LoaderManager framework - it takes care of reloading the data with the initiated Loaders in case the enclosing Activity or Fragment get re-created.

实际上,Loaders的某些实现方式不会重新加载数据,而只是提供对内部缓存的最新数据的访问.

In fact, some implementations of Loaders do not reload the data, but simply provide access to the latest data that has been cached internally.

底线:您观察到LoaderManager框架的正确行为.

Bottom line: you observe correct behavior of LoaderManager framework.

不清楚您要使用Loader完成什么,但是看起来您选择的工具不正确.如果您的目标是仅执行一次操作,则应使用AsyncTask而不是Loaders.

It is not clear what it is you're trying to accomplish with your Loader, but it looks like you chose an incorrect tool. If your goal is to perform the action just once, then you should use AsyncTask instead of Loaders.

这篇关于活动重启时Asynctaskloader重新加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆