向上导航会重新加载加载器,但向后导航不会
Up navigation reloads loader but back navigation doesn't
我正在使用 AsyncTaskLoader 将来自 HTTPRequest 的数据加载到 ArrayList 中。加载后,数据将通过回收器视图显示为列表。当我单击列表中的一项时,将触发 activity B 显示该数据的详细屏幕。然后,我有两个选项可以返回列表,一个是通过后退按钮 (phone),另一个是通过工具栏上的向上按钮 <- 因为它 android.support.PARENT_ACTIVITY
实现了 avtivity B。
好吧,后退按钮不会触发加载器,但向上按钮会重新加载整个东西。到底是怎么回事?我希望两者的行为相同,即不像我在 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);
}}
在ActivityA的onCreate
中,我有这样的loader调用
`LoaderManager loaderManager = getLoaderManager();
loaderManager.initLoader(0, null, 这个);
然后,我实现接口:
@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
将被再次调用,因为当您 按下返回时加载程序知道 activity states.Now phone、activity 的按钮 将简单地置于最前面,但如果您 按后退按钮 在 工具栏 中,先前的 activity 将被 再次创建 ,因此您的加载器将被重新初始化并且 if (mFalla == null)
将变为 true ,导致调用 forceLoad()
。
您可以在 activity B
中明确处理工具栏的后退按钮单击以避免此行为。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
onBackPressed();
}
return true;
}
我正在使用 AsyncTaskLoader 将来自 HTTPRequest 的数据加载到 ArrayList 中。加载后,数据将通过回收器视图显示为列表。当我单击列表中的一项时,将触发 activity B 显示该数据的详细屏幕。然后,我有两个选项可以返回列表,一个是通过后退按钮 (phone),另一个是通过工具栏上的向上按钮 <- 因为它 android.support.PARENT_ACTIVITY
实现了 avtivity B。
好吧,后退按钮不会触发加载器,但向上按钮会重新加载整个东西。到底是怎么回事?我希望两者的行为相同,即不像我在 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);
}}
在ActivityA的onCreate
中,我有这样的loader调用
`LoaderManager loaderManager = getLoaderManager(); loaderManager.initLoader(0, null, 这个);
然后,我实现接口:
@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
将被再次调用,因为当您 按下返回时加载程序知道 activity states.Now phone、activity 的按钮 将简单地置于最前面,但如果您 按后退按钮 在 工具栏 中,先前的 activity 将被 再次创建 ,因此您的加载器将被重新初始化并且 if (mFalla == null)
将变为 true ,导致调用 forceLoad()
。
您可以在 activity B
中明确处理工具栏的后退按钮单击以避免此行为。
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home){
onBackPressed();
}
return true;
}