android:进度DialogFragment不会在AsyncTask的开头显示
我正在使用兼容性库 v4+。 关注其他线程< /a> 和 开发指南 我正在尝试创建一个片段,从创建它的活动中调用进度对话框。 该方法使用 DialogFragment 在 AsyncTask 的预执行中显示并关闭(删除)在执行后。 结果我没有得到任何对话框......好吧,事实是对话框只在 thread.sleep 结束时显示几分之一秒。
有人能告诉我哪里错了吗?
我在这里有我的片段:
public class GroupsDetailsFragment extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_groups_details, container, false);
return v;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
loadPage();
}
public void loadPage() {
Log.i(TAG, "loadPage");
new LoadPageTask(getBaseActivity()).execute();
}
public void executeLoadPagePreExecution() {Log.i(TAG, "executeLoadPagePreExecution"); }
public void executeLoadPageInBackground() {Log.i(TAG, "executeLoadPageInBackground"); }
public void executeLoadPagePostExecution() {try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}Log.i(TAG, "executeRefreshPostExecution"); }
protected class LoadPageTask extends AsyncTask<Void, Void, String> {
private Context context;
public LoadPageTask(Context context) {
super();
this.context = context;
}
@Override
protected void onPreExecute() {
// before the network request begins, show a progress indicator
getBaseActivity().showLoadingProgressDialog(context);
executeLoadPagePreExecution();
}
@Override
protected String doInBackground(Void... params) {
String result = executeLoadPage();
Log.i(TAG, "error message result: " + result);
return result;
}
// Returns null if Everything is OK. Error Message if any problem.
private String executeLoadPage() {
try {
executeLoadPageInBackground();
return null;
} catch (Exception e){
Log.w(TAG, e.getMessage(), e);
return(e.getMessage());
}
}
@Override
protected void onPostExecute(String message) {
executeLoadPagePostExecution();
getBaseActivity().dismissProgressDialog();
if (message!=null){
Utility.showMessageAlertDialog(message, context);
}
}
}
}
ProgressDialogFragment 是显示片段的 FragmentActivity 的内部类:
public static class ProgressDialogFragment extends DialogFragment {
public static ProgressDialogFragment newInstance(String title, String message) {
ProgressDialogFragment fragment = new ProgressDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
fragment.setArguments(args);
return fragment;
}
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String message = getArguments().getString("message");
ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle(title);
progressDialog.setMessage(message);
progressDialog.show();
return progressDialog;
}
}
public void showProgressDialog(String title, String message) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment prev = fragmentManager.findFragmentByTag("progress dialog");
if(prev!=null) {
fragmentTransaction.remove(prev);
}
fragmentTransaction.addToBackStack(null);
DialogFragment newFragment = ProgressDialogFragment.newInstance(title, message);
newFragment.show(fragmentManager, "progress dialog");
//fragmentTransaction.commit();
}
public void removeProgressDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment prev = fragmentManager.findFragmentByTag("progress dialog");
if(prev!=null) {
fragmentTransaction.remove(prev);
}
fragmentTransaction.commit();
}
I´m using the compatibility library v4+.
Following other threads and the dev guide I´m trying to create a fragment that calls a progress dialog from the activity that creates it.
The approach uses DialogFragment displayed in the pre execute of an AsyncTask and dismissed (removed) on the post execute.
As result I don´t get the any dialog.... well, the truth is that the dialog gets displayed only for a fraction of second at the end of the thread.sleep.
Can someone tell me where I´m wrong?
I have my fragment here:
public class GroupsDetailsFragment extends BaseFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_groups_details, container, false);
return v;
}
@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
loadPage();
}
public void loadPage() {
Log.i(TAG, "loadPage");
new LoadPageTask(getBaseActivity()).execute();
}
public void executeLoadPagePreExecution() {Log.i(TAG, "executeLoadPagePreExecution"); }
public void executeLoadPageInBackground() {Log.i(TAG, "executeLoadPageInBackground"); }
public void executeLoadPagePostExecution() {try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}Log.i(TAG, "executeRefreshPostExecution"); }
protected class LoadPageTask extends AsyncTask<Void, Void, String> {
private Context context;
public LoadPageTask(Context context) {
super();
this.context = context;
}
@Override
protected void onPreExecute() {
// before the network request begins, show a progress indicator
getBaseActivity().showLoadingProgressDialog(context);
executeLoadPagePreExecution();
}
@Override
protected String doInBackground(Void... params) {
String result = executeLoadPage();
Log.i(TAG, "error message result: " + result);
return result;
}
// Returns null if Everything is OK. Error Message if any problem.
private String executeLoadPage() {
try {
executeLoadPageInBackground();
return null;
} catch (Exception e){
Log.w(TAG, e.getMessage(), e);
return(e.getMessage());
}
}
@Override
protected void onPostExecute(String message) {
executeLoadPagePostExecution();
getBaseActivity().dismissProgressDialog();
if (message!=null){
Utility.showMessageAlertDialog(message, context);
}
}
}
}
And ProgressDialogFragment which is inner class of the FragmentActivity that dispays the fragment:
public static class ProgressDialogFragment extends DialogFragment {
public static ProgressDialogFragment newInstance(String title, String message) {
ProgressDialogFragment fragment = new ProgressDialogFragment();
Bundle args = new Bundle();
args.putString("title", title);
args.putString("message", message);
fragment.setArguments(args);
return fragment;
}
@Override
public ProgressDialog onCreateDialog(Bundle savedInstanceState) {
String title = getArguments().getString("title");
String message = getArguments().getString("message");
ProgressDialog progressDialog = new ProgressDialog(getActivity());
progressDialog.setTitle(title);
progressDialog.setMessage(message);
progressDialog.show();
return progressDialog;
}
}
public void showProgressDialog(String title, String message) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment prev = fragmentManager.findFragmentByTag("progress dialog");
if(prev!=null) {
fragmentTransaction.remove(prev);
}
fragmentTransaction.addToBackStack(null);
DialogFragment newFragment = ProgressDialogFragment.newInstance(title, message);
newFragment.show(fragmentManager, "progress dialog");
//fragmentTransaction.commit();
}
public void removeProgressDialog() {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment prev = fragmentManager.findFragmentByTag("progress dialog");
if(prev!=null) {
fragmentTransaction.remove(prev);
}
fragmentTransaction.commit();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
通常您不应该暂停 UI 线程。那么将 Thread.Sleep() 移至 doInBackground() 怎么样?这对你有用吗?
Usually you should not pause the UI thread. So how about moving the Thread.Sleep() into doInBackground()? Does this work for you?
doInBackGround 是在工作线程(只是不是 UI 线程)上完成工作的地方,onPostExacute 是与主 UI 对话的地方..这就是为什么
doInBackGround is where the work is done on a worker thread (just not the UI thread), onPostExacute is what talks to the main UI.. that's why
我认为你不应该
在 onCreateDialog(...) 中调用 ProgressDialog.show()
I don't think you should be calling
progressDialog.show() in onCreateDialog(...)