在我的MainActivity
我有一个登录名view
。当我在 中检查用户名和密码时AsyncTask
,我显示一个ProgressDialog
.
我在另一个问题中问了如何不重置AsyncTask
,我用 实现了它AndroidManifest.xml
,但是我需要保存用户名和密码,以便在我改变方向时不会被删除,但是onSaveInstanceState
没有onRestoreInstanceState
调用方法。
我认为这个问题一定与android:configChanges
in 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>