0

能够成功地从服务器获取数据。如果数据成功,则导航到 NextScreen。1. 如果我在数据出现之前按下后退按钮,那么 Asyntask 必须消失,它不应该导航到 NextScreen。它必须只停留在 Screenone。

但是如果我按下后退按钮 dailog 在几秒钟后消失,它会导航到 NextScreen.so 如何阻止它。

public class Screenone extends Activity {
    EditText user,pass;
    Button login;           
    InputStream is;
    String productResponse;
    EditText edit;   
    int responseCode;               

   @Override
   protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);


    requestWindowFeature(Window.FEATURE_NO_TITLE);  
    setContentView(R.layout.mainlog);       

    user = (EditText) findViewById(R.id.user);
    pass =  (EditText) findViewById(R.id.password);     
    login = (Button) findViewById(R.id.login);

   }    

    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub      

            if ((user.getText().length() > 0)
                    && (pass.getText().length() > 0) ) {                
                new Load().execute(Status);
            }
                    }
    });

   }


   private class Load extends AsyncTask<String, String, String> {

    private ProgressDialog Dialog = new ProgressDialog(LoginScreen.this);
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        Dialog.setMessage("Please wait...");
        Dialog.show();
    }

    protected String doInBackground(String... status) { 
                try {
            HttpClient httpclient = new DefaultHttpClient();                     

     HttpPost post = new HttpPost("---url---");                     
            responseCode =httpResponse.getStatusLine().getStatusCode();

        }           


        catch (Exception e) {           
                            }                     
        return null;
    }


    protected void onPostExecute(String Result) {
        // TODO Auto-generated method stub
        super.onPostExecute(Result);      

            if (responseCode==200) {            
            Dialog.dismiss();           

            Intent intent =new Intent(Screenone.this, NextScreen.class);
            startActivity(intent);                          
                        }           

                            }               
            }


  public void onBackPressed() {
    // TODO Auto-generated method stub
    super.onBackPressed();

     LoginScreen.Load xx = new LoginScreen().new Load();

     if(new Load()!=null){
         if(!(new Load().getStatus() == AsyncTask.Status.FINISHED)){

            httpclient.getConnectionManager().shutdown();

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

             if(new Load().getStatus() == AsyncTask.Status.RUNNING){
                 new Load().cancel(true);
              }
          }


     }
   }

   }
4

1 回答 1

0

在 AsyncTask 取消后,您需要在 Back Button Click 上启动下一个 Activity:

 if(new Load().getStatus() == AsyncTask.Status.RUNNING){
      new Load().cancel(true);

      // start  Next Activity here...
      Intent intent =new Intent(Screenone.this, NextScreen.class);
      startActivity(intent); 

   }
于 2013-02-15T10:13:26.473 回答