不要重置AsyncTask并使用onSaveInstanceState

时间:2013-08-29 10:57:04

标签: android android-asynctask

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

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

我认为问题必须与onRestoreInstanceState中的android:configChanges相关。你能救我吗?

这是我的代码:

MainActivity.java

AndroidManifest

的AndroidManifest.xml

//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;
    }
}

2 个答案:

答案 0 :(得分: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
    }

    }

答案 1 :(得分:0)

使用android:configChanges="orientation|screenSize",您告诉系统您自己处理了方向更改。这导致您的活动未重新启动,因此在方向更改时onSaveInstanceStateonRestoreInstanceState未被调用。请改用onConfigurationChanged