2013-08-29 19 views
0

在我的MainActivity我有一個登錄view。當我在AsyncTask中檢查用戶名和密碼時,我會顯示一個ProgressDialog不要重置AsyncTask並使用onSaveInstanceState

我問另外一個問題如何不正在重置AsyncTask和我一起AndroidManifest.xml實現它,但我需要保存的用戶名和密碼不是如果我改變方向,但onSaveInstanceStateonRestoreInstanceState方法不會被調用被刪除。

我認爲這個問題必須與android:configChangesAndroidManifest有關。你可以幫我嗎?

這裏是我的代碼:

MainActivity.java

//Method called when login Button is pressed 
public void entrar(View view) {  
    /* Escondemos el teclado */ 
    InputMethodManager imm = (InputMethodManager)getSystemService(
        Context.INPUT_METHOD_SERVICE); 
    imm.hideSoftInputFromWindow(editTextUsuario.getWindowToken(), 0); 
    /* Comprobamos si hay conexión a Internet */ 
    if(myApplication.isOnline()) { 
     LoadingMainTask myWebFetch = new LoadingMainTask(this); 
     myWebFetch.execute(); 
    } 
    /* Si no se dispone de conexión a Internet, mostramos un error */ 
    else { 
     myApplication.mostrarMensaje(this, R.string.error_conexion_titulo, 
        R.string.error_conexion_mensaje); 
    } 
} 

private class LoadingMainTask extends AsyncTask<String, Void, Boolean> { 
    private ProgressDialog dialog; 
    private volatile boolean running = true; 
    private MainActivity activity; 
    TuplaUsuario tuplaUsuario = new TuplaUsuario(); 

    private LoadingMainTask(MainActivity activity) { 
     this.activity = activity; 
     context = activity; 
     dialog = new ProgressDialog(context); 
     dialog.setCancelable(true); 
     dialog.setOnCancelListener(new OnCancelListener() { 
      @Override 
      public void onCancel(DialogInterface dialog) { 
       cancel(true); 
      } 
     }); 
    } 

    /** application context. */ 
    private Context context; 

    @Override 
    protected void onCancelled() { 
     running = false; 
    } 

    @Override 
    protected void onPreExecute() { 
     this.dialog.setMessage(getString(R.string.loading)); 
     this.dialog.show(); 

    } 

    @Override 
    protected void onPostExecute(final Boolean success) { 
     if (dialog.isShowing()) { 
      dialog.dismiss(); 
     } 

     if (success) { 
      /* Si el login no es correcto, mostramos un error por pantalla */ 
      if (!tuplaUsuario.getOk()) { 
       myApplication.mostrarMensaje(activity, R.string.error_datos_login_titulo, 
         tuplaUsuario.getMensaje()); 
      } 
      /* Entrar */ 
      else { 
       Intent intent = new Intent(activity, TabsFacturasActivity.class); 
       startActivity(intent); 
      } 
     } else { 
      System.out.println("throw exception post"); 
      myApplication.throwException(activity); 
     } 
    } 

    @Override 
    protected Boolean doInBackground(final String... args) { 
     while (running) { 
      try{  
       String usuario = String.valueOf((editTextUsuario).getText()); 
       String password = String.valueOf((editTextPassword).getText()); 

       /* Check login */ 
           } 
       return true; 
      } catch (Exception e){ 
       return false; 
      } 
     } 
     return null; 
    } 
} 

AndroidManifest.xml中

<application 
     android:allowBackup="true" 
     //... 
     android:configChanges="orientation" > 
     <activity 
      //This next two lines are what I was said to use and that works. Other answers were not useful 
      android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" 
      android:windowSoftInputMode="adjustPan" 
      android:theme="@style/AppTheme" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
</application> 

回答

0

正如你在android系統添加定位:configChanges它不完全重新啓動正在運行的Activit y幫助它適應這種變化。所以,如果你需要保存用戶名和密碼覆蓋下面的方法在您的MainActivity

@Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 

if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT || newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) 
    { 
     //do whatever you want 
    } 

    } 
+0

我應該如何將數據保存在視圖中以便在更改方向後進行恢復?因爲'onConfigurationChanged'沒有'Bundle'。例如,我可以使用'mTextView.getText()',但是如何使用'setText()'? – Lyd

+0

您可以緩存目錄以保存您的數據臨時文件。這裏是一個很好的例子http://wptrafficanalyzer.in/blog/android-temporarily-writing-and-reading-files-to-cache-directory/ –

+0

我已經解決了,創建一個新的Bundle,添加數據到它並用它調用'onCreate'。 – Lyd

0

隨着android:configChanges="orientation|screenSize"你告訴你的系統,你處理的取向對自己的改變。這會導致您的活動未重新啓動,因此在更改方向時不會調用onSaveInstanceStateonRestoreInstanceState。改爲使用onConfigurationChanged