向上导航刷新加载器,但返回导航不
问题描述:
我正在使用AsyncTaskLoader将数据加载到来自HTTPRequest的ArrayList中。一旦加载,数据将通过回收站视图显示为列表。当我点击列表中的一个项目时,活动B被触发,显示该数据的详细屏幕。然后,我有两个选项可以回到列表中,一个是通过后退按钮(电话),另一个是通过工具栏上的向上按钮< -,因为avtivity B具有android.support.PARENT_ACTIVITY
实施。向上导航刷新加载器,但返回导航不
那么,后退按钮不会触发加载程序,但上按钮重新加载整个事情。到底是怎么回事?我希望两者行为相同,即不要按照我在onStartLoading()
中指定的方式重新加载。
这是我的AsynTask装载机被称为像往常一样,通过实现LoaderCallbacks<List<T>>
接口
public class FallaLoader extends AsyncTaskLoader<List<Falla>> {
private String mUrl;
private List<Falla> mFalla;
FallaLoader(Context context, String url)
{
super(context);
mUrl = url;
}
@Override
protected void onStartLoading()
{
if (mFalla == null) {
// we have no data, so kick off loading
forceLoad();
}
else {
// use cached data, fallas won't change for a year, so... just needed everytime I start
deliverResult(mFalla);
}
}
// This happens in the Background thread
@Override
public List<Falla> loadInBackground()
{
if (mUrl == null)
{
return null;
}
// Perform the network request, parse the response, and extract a list of earthquakes.
// pass the context since it will be needed to get the preferences
return Utils.fetchFallasData(mUrl, getContext());
}
@Override
public void deliverResult(List<Falla> data)
{
// We’ll save the data for later retrieval
mFalla = data;
super.deliverResult(data);
}}
在活动A的onCreate
,我有呼叫到装载机这样
`LoaderManager loaderManager = getLoaderManager (); loaderManager.initLoader(0,null,this);
,然后,我实现接口:
@Override
public Loader<List<Falla>> onCreateLoader(int i, Bundle bundle)
{
return new FallaLoader(this, F_URL);
}
@Override
public void onLoadFinished(Loader<List<Falla>> loader, List<Falla> fallas)
{
View loadingIndicator = findViewById(R.id.loading_indicator);
loadingIndicator.setVisibility(View.GONE);
mEmptyStateTextView.setText(R.string.no_fallas);
if (fallas != null && !fallas.isEmpty())
{
adapter.swap(fallas);
}
}
@Override
public void onLoaderReset(Loader<List<Falla>> loader) {
}
`
谢谢!
答
当你来自activity B
回来,onStartLoading
将再次被称为装载机知道活动的states.Now当你按后退按钮电话的,活动将被简单地带到前面,但如果你按回退按钮在工具栏,先前的活动将再次创建,因此您的加载程序将被重新初始化和if (mFalla == null)
将成为真实的,导致致电forceLoad()
。
您可以在activity B
中明确处理工具栏的后退按钮,以避免此行为。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
onBackPressed();
}
return true;
}
发布您主要处理数据和返回按钮的活动。 – mallaudin