从 child 任务返回后执行后台任务时出错
Error on doing a background task after returning from child task
我不确定我是否完全理解从 child activity 返回后需要做什么来检索数据。我不需要将数据传递给 parent activity。我使用了 this for reference, they do mention onPause and onResume as well as possible solutions but should I use saved preferences instead of onSaveInstanceState and onRestoreInstanceState? It fails at the background task of checking the boolean. I also used this this 以获得进一步的帮助。
代码如下:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putString("type",type);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
super.onRestoreInstanceState(savedInstanceState);
type = savedInstanceState.getString("type");
}
public class getFeed extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
ParseQuery<ParseObject> contentFeed = new ParseQuery<>("UserCommentary");
if(type.equals("object1")){
//Query for object1
} catch (ParseException e) {
e.printStackTrace();
}
}
else if(type.equals("object2")){
//Query for object2
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mySwipeRefreshLayout.setRefreshing(false);
commentary.setAdapter(vCommentDetailAdapter);
vCommentDetailAdapter.notifyDataSetChanged();
}
编辑:向代码添加了更多信息
事后看来,您的评论是 getFeed
AsyncTask 正在 onCreate()
中执行。
你基本上是调用得太早了。请参阅 here 了解 Android Activity 生命周期。
onCreate()
/ onStart()
在 onRestoreInstanceState()
之前被调用。所以把任务的执行放在你的onResume()
方法中。
我发现这个 solution 完成了我需要的,不需要保存数据和重新加载数据。
我不确定我是否完全理解从 child activity 返回后需要做什么来检索数据。我不需要将数据传递给 parent activity。我使用了 this for reference, they do mention onPause and onResume as well as possible solutions but should I use saved preferences instead of onSaveInstanceState and onRestoreInstanceState? It fails at the background task of checking the boolean. I also used this this 以获得进一步的帮助。
代码如下:
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putString("type",type);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
super.onRestoreInstanceState(savedInstanceState);
type = savedInstanceState.getString("type");
}
public class getFeed extends AsyncTask<String, Void, Void>{
@Override
protected Void doInBackground(String... strings) {
ParseQuery<ParseObject> contentFeed = new ParseQuery<>("UserCommentary");
if(type.equals("object1")){
//Query for object1
} catch (ParseException e) {
e.printStackTrace();
}
}
else if(type.equals("object2")){
//Query for object2
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
mySwipeRefreshLayout.setRefreshing(false);
commentary.setAdapter(vCommentDetailAdapter);
vCommentDetailAdapter.notifyDataSetChanged();
}
编辑:向代码添加了更多信息
事后看来,您的评论是 getFeed
AsyncTask 正在 onCreate()
中执行。
你基本上是调用得太早了。请参阅 here 了解 Android Activity 生命周期。
onCreate()
/ onStart()
在 onRestoreInstanceState()
之前被调用。所以把任务的执行放在你的onResume()
方法中。
我发现这个 solution 完成了我需要的,不需要保存数据和重新加载数据。