【问题标题】:How to dismiss progress dialog when i'm moving to another activity?当我移动到另一个活动时如何关闭进度对话框?
【发布时间】:2014-10-10 08:00:51
【问题描述】:

我在 OnPostExecute 开始一个新的 Activity 并调用 pDialog.dismiss() ,但是当我从新的 Activity 回到上一个 Activity 时 - 进度对话框仍在运行。 当我转移到另一项活动时,如何永久解除它?

这是我的 AsyncTask 类。

public class FindRideTask extends AsyncTask<Void, Void, Void>
{
     private ProgressDialog pDialog;

    @Override
    protected void onPostExecute(Void result) {

        if(pDialog!=null && pDialog.isShowing())
        {
            pDialog.dismiss();
        }
        Intent intent = new Intent(getApplicationContext(), FindRideBoard.class);
        Bundle bundle = new Bundle();
        bundle.putParcelableArrayList("Results", ridesList);
        intent.putExtras(bundle);
        startActivity(intent);

    }


    @Override
    protected void onPreExecute() {

        pDialog = new ProgressDialog(FindRide.this);
        pDialog.show(FindRide.this, "מתחבר לשרת", "אנא המתן");
    }


    @Override
    protected Void doInBackground(Void... params) {
        s_ParseLogic = ParseLogic.getInstance();

        try
        {
            ArrayList<ParseObject> ridesFromQuery = (ArrayList<ParseObject>)s_ParseLogic.FindRides(sourceList.getText().toString(), destinationList.getText().toString(), time_picker.getText().toString(), m_date.getText().toString());
            ridesList = new ArrayList<Ride>();

            for(int i = 0 ; i < ridesFromQuery.size() ; i++)
            {
                ParseObject obj = ridesFromQuery.get(i);
                ridesList.add(new Ride((String)obj.get("Source"), (String)obj.get("Destination"), (String)obj.get("Day"),(String) obj.get("Time"), (String)obj.get("IsPermanent"), (String)obj.get("Driver"), (String)obj.get("Driver Phone"))) ;
            }
        }
        catch(ParseException e)
        {
        }

        return null;
    }

}

【问题讨论】:

  • 在 postexecute() 的意图调用之前使用 pDialog.dismiss();
  • pDialog.dismiss(); 应该是您在onPostExecute() 中的第一条语句
  • 我以前试过这个。它仍在运行:(
  • 这个 AsyncTask 是 Activity 中的内部类吗?您确定在之前的活动中没有显示任何其他进度对话框吗?复查一次
  • 是的。它是活动中的内部类。这个进度对话框是我唯一拥有的,它在 AsyncTask 类中定义。

标签: android android-asynctask progressdialog


【解决方案1】:

这里的getApplicationContext() 是您的ApplicationContext。所以用

改变
  pDialog = new ProgressDialog(YourActivityName.this);

onPostExecute()方法中

  @Override
  protected void onPostExecute(Void result) {

   if(pDialog!=null && pDialog.isShowing()){

     pDialog.dismiss();
    }

    Intent intent = new Intent(getApplicationContext(), FindRideBoard.class);
    Bundle bundle = new Bundle();
    bundle.putParcelableArrayList("Results", ridesList);
    intent.putExtras(bundle);
    startActivity(intent);

}

UPDATE:

在您的ActivityonDestroy() 方法中销毁您的ProgressDialog

 @Override
 protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
     if(pDialog!=null && pDialog.isShowing()){

     pDialog.dismiss();
    }
 }

【讨论】:

  • 你的 AsyncTask 是一个单独的类吗?
  • 我的 AsyncTask 是我的 Activity 中的一个类,它继承自 FragmentActivity
  • @Sharon182 所以在某个地方你遗漏了一些东西或一些错误,因为通常我也使用这种方式,但我没有遇到过这种类型的问题。因此,只需逐步检查您的代码。
  • 如果我不带任何参数调用 pDialog.show(),问题就解决了。有什么问题的线索吗?
  • 是的,因为当您使用pDialog = new ProgressDialog(FindRide.this); 时,您必须单独设置消息和标题,而此时使用pDialog.show()。另一种方式是使用pDialog = new pDialog.show(FindRide.this, "מתחבר לשרת", "אנא המתן");
【解决方案2】:

你可以像@X'Factor 说的那样做,但是在你的活动的 onStop() 方法中关闭对话框,当活动不再可见时会被调用。

@Override
 protected void onStop() {
    super.onStop();
     if(pDialog!=null && pDialog.isShowing()){

     pDialog.dismiss();
    }
 }

还要检查您是否在 Activity 的 onResume() 中启动 AsyncTask,这可能会导致对话框首先出现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-11
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 2019-07-02
    相关资源
    最近更新 更多