2

我有一个带有登录屏幕的应用程序。检查登录是否正常时,我显示“正在加载” Dialog

如果我改变屏幕方向,Dialog消失并且我认为登录检查再次开始,但之后应用程序崩溃。

我已经阅读了一些解决方案,但我无法做到。

这是我的代码:

MainActivity.java

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) {
            try{    
                String usuario = String.valueOf((editTextUsuario).getText());
                String password = String.valueOf((editTextPassword).getText());

                /* Comprobar datos del login */
                try {
                    tuplaUsuario = myApplication.getDatosUsuario(usuario, password, loginGuardado);
                } catch (JSONException e) {
                    myApplication.throwException(activity);
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    myApplication.throwException(activity);
                    e.printStackTrace();
                } catch (IOException e) {
                    myApplication.throwException(activity);
                    e.printStackTrace();
                }
                /* Si el login es correcto, nos guardamos su login, descargamos el resto 
                 * de información y entramos */
                if (tuplaUsuario.getOk()) {
                    boolean rememberMe = myApplication.getRememberMe();
                    if (rememberMe) {
                        /* Si el usuario ya estaba guardado, no hace falta volver a guardarlo */
                        if(!loginGuardado) {
                            myApplication.guardarUsuario(activity, usuario, password);
                        }
                    }
                }
                return true;
             } catch (Exception e){
                return false;
             }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" ... >

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/StyledIndicators"
        android:configChanges="orientation"
        android:name="com.example.factorenergia.MyApplication" >
        <activity
            android:name="com.example.factorenergia.MainActivity"
            android:label="@string/app_name"
            android:configChanges="orientation"
            android:theme="@style/AppTheme"  >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        ...

        </application>

</manifest>
4

2 回答 2

2

在你的清单中试试这个......

<activity android:name="Your Activity Name"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:windowSoftInputMode="adjustPan"/>
于 2013-08-28T12:15:29.617 回答
0

Android 在每次方向更改时重新创建 Activity。为了保持当前活动状态,您需要保存instance state并在可能的情况下稍后检索它。

参考本教程: http: //www.intertech.com/Blog/saving-and-retrieving-android-instance-state-part-1/

虽然更简单的解决方案是禁用此屏幕上的方向更改。为此,您需要将以下行添加到AndroidManifest.xml.

android:screenOrientation="portrait"
于 2013-08-28T12:07:55.407 回答