【问题标题】:Display ProgressDialog while opening new Fragment and AsyncTask打开新的 Fragment 和 AsyncTask 时显示 ProgressDialog
【发布时间】:2015-04-26 09:22:15
【问题描述】:

我有一个导航抽屉,它打开了几个片段,其中一个使用 AsyncTask 执行后台任务以加载一些数据,同时显示一个 ProgressDialog。

这里的问题是 ProgressDialog “几乎”没有显示。我说“几乎”是因为它只显示了很短的时间。

我要实现的是:点击Navigation Drawer项,关闭Navigation Drawer,显示ProgressDialog,用AsyncTask打开Fragment,做后台数据,关闭ProgressDialog,查看数据。

它现在在做的是:点击导航抽屉项目,被冻结,关闭导航抽屉,非常快速地显示和关闭ProgressDialog,查看数据。

我不知道它为什么这样做,这肯定是我不明白的。这是我的代码:

主活动:

public class MainActivity extends FragmentActivity {

    private ProgressDialog progressDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ...

        progressDialog = new ProgressDialog(this);

        // ...
    }

    @OnClick(R.id.history_menu_item)
    protected void historyMenuItemClick() {
        progressDialog.setMessage(getString(R.string.history_fragment_crossing_call_log));
        progressDialog.show();

        drawerLayout.closeDrawers();
        switchToFragment(new HistoryFragment());
    }

    public void switchToFragment(final Fragment fragment) {
        final FragmentManager fragmentManager = getSupportFragmentManager();

        final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.content_frame, fragment);
        fragmentTransaction.commit();
    }
}

历史片段:

public class HistoryFragment extends Fragment implements ActionBar.OnNavigationListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        // ...

        updateCalls();

        // ...
    }

    public void updateCalls() {
        // ...

        new ShowHistoryCallLog().execute().get();

        // ...
    }

    private final class ShowHistoryCallLog extends AsyncTask<Void, Void, Cursor> {

        // ...

        @Override
        protected void onPostExecute(Cursor cursor) {

            // ...

            final ProgressDialog progressDialog = ((MainActivity) getActivity()).getProgressDialog();

            if (progressDialog != null && progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }
    }
}

【问题讨论】:

    标签: android android-fragments android-asynctask navigation-drawer progressdialog


    【解决方案1】:

    您应该尝试将 UI 和后台工作分开一点。我不知道这是否是最佳做法,但我使用LocalBroadcastManager 来实现。

    在 Fragment 或 Activity 中,您可以像这样注册一个新的接收器:

    LocalBroadcastManager.getInstance(this).registerReceiver(new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                refreshUI();
                //or
                (show/dismiss)ProgressDialog();
            }
        }, new IntentFilter("<any key>")"));
    

    如果你现在想刷新你的用户界面,你可以在代码中的任何地方调用类似的东西:

    LocalBroadcastManager.getInstance(this).sendBroadcast(new Intent("<any key>"));
    

    您现在可以在 Fragment 和任务的 onPreExecute 中注册接收器,只需发送一条消息以显示您的 ProgressDialog。要关闭对话框,只需在 onPostExecute 中发送一条消息将其关闭。可能还有其他方法可以实现这一点,但这对我来说效果很好。

    【讨论】:

      猜你喜欢
      • 2014-07-25
      • 2012-08-13
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 2012-08-13
      • 2015-02-14
      • 1970-01-01
      相关资源
      最近更新 更多