在我的MainActivity
我有一個登錄view
。當我在AsyncTask
中檢查用戶名和密碼時,我會顯示一個ProgressDialog
。不要重置AsyncTask並使用onSaveInstanceState
我問另外一個問題如何不正在重置AsyncTask
和我一起AndroidManifest.xml
實現它,但我需要保存的用戶名和密碼不是如果我改變方向,但onSaveInstanceState
和onRestoreInstanceState
方法不會被調用被刪除。
我認爲這個問題必須與android:configChanges
在AndroidManifest
有關。你可以幫我嗎?
這裏是我的代碼:
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>
我應該如何將數據保存在視圖中以便在更改方向後進行恢復?因爲'onConfigurationChanged'沒有'Bundle'。例如,我可以使用'mTextView.getText()',但是如何使用'setText()'? – Lyd
您可以緩存目錄以保存您的數據臨時文件。這裏是一個很好的例子http://wptrafficanalyzer.in/blog/android-temporarily-writing-and-reading-files-to-cache-directory/ –
我已經解決了,創建一個新的Bundle,添加數據到它並用它調用'onCreate'。 – Lyd