2014-10-10 67 views
0

我开始一个新的活动OnPostExecute并致电pDialog.dismiss(),但是当我从新的活动回到上一个活动时 - 进度对话框仍在运行。 在我转移到其他活动的那一刻,我该如何永久解雇它?如何在我转移到其他活动时关闭进度对话框?

这是我的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; 
    } 

} 
+1

在postexecute()的意图调用之前使用'pDialog.dismiss();'。 – prakash 2014-10-10 08:02:05

+1

'pDialog.dismiss();'应该是'onPostExecute()'的第一条语句' – 2014-10-10 08:03:08

+0

我以前试过这个。它仍在运行:( – Sharon182 2014-10-10 08:07:54

回答

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

摧毁你ProgressDialogonDestroy()Activity的方法。

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

    pDialog.dismiss(); 
    } 
} 
+0

不幸的是,同样的问题... – Sharon182 2014-10-10 08:14:46

+0

这是非常奇怪的 – Sharon182 2014-10-10 08:15:07

+0

是您的AsyncTask是一个独立的类? – Piyush 2014-10-10 08:34:19

0

你可以像@ X'Factor说,但驳回您的活动的的onStop()methodm对话框当活动不再可见它被调用。

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

    pDialog.dismiss(); 
    } 
} 

还要检查,如果你已经开始在活动的的onResume()您的AsyncTask这可能导致对话出现在首位。