0

在我的MainActivity我有一个登录名view。当我在 中检查用户名和密码时AsyncTask,我显示一个ProgressDialog.

我在另一个问题中问了如何不重置AsyncTask,我用 实现了它AndroidManifest.xml,但是我需要保存用户名和密码,以便在我改变方向时不会被删除,但是onSaveInstanceState没有onRestoreInstanceState调用方法。

我认为这个问题一定与android:configChangesin 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>
4

2 回答 2

0

当您在 android:configChanges 中添加方向时,它不会完全重新启动正在运行的 Activity 以帮助它适应此类更改。因此,如果您需要保存用户名和密码,请在 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
    }

    }
于 2013-08-29T11:12:28.663 回答
0

android:configChanges="orientation|screenSize"你告诉你的系统你自己处理方向的改变。这会导致您的活动没有重新启动,因此在onSaveInstanceState方向onRestoreInstanceState改变的情况下不会被调用。改为使用onConfigurationChanged

于 2013-08-29T11:14:04.323 回答