asyncTask的后退按钮

时间:2013-02-15 10:01:45

标签: android android-asynctask

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

但是如果我按下后退按钮就会在几秒钟之后消失,它会导航到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);
              }
          }


     }
   }

   }

1 个答案:

答案 0 :(得分:0)

您将需要在后退按钮上启动下一个活动在AsyncTask取消后点击:

 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); 

   }