2013-02-15 40 views
0

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

但如果我按几秒钟后backbutton 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); 
       } 
      } 


    } 
    } 

    } 

回答

0

你需要开始后退按钮单击Next活动后的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); 

    } 
+0

实际上是happpening它被导航到NextScreen.it不应该是这样的。如果我按回按钮,它必须保留在ScreenOne中。 – user2042881 2013-02-15 10:35:22

+0

@ user2042881:在'new Load()。cancel(true);'行并检查它是否正在执行中,将日志放在onBackPressed中 – 2013-02-15 10:40:05

+0

不是它正在执行日志 – user2042881 2013-02-15 10:45:45