android:进度DialogFragment不会在AsyncTask的开头显示

发布于 2025-01-04 02:00:40 字数 5056 浏览 1 评论 0原文

我正在使用兼容性库 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮色兮凉城 2025-01-11 02:00:40

通常您不应该暂停 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?

鱼忆七猫命九 2025-01-11 02:00:40

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

无人问我粥可暖 2025-01-11 02:00:40

我认为你不应该

在 onCreateDialog(...) 中调用 ProgressDialog.show()

I don't think you should be calling

progressDialog.show() in onCreateDialog(...)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文