Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/188.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在onSaveInstanceState一次保存所有布尔人?_Android - Fatal编程技术网

Android 如何在onSaveInstanceState一次保存所有布尔人?

Android 如何在onSaveInstanceState一次保存所有布尔人?,android,Android,调用onSaveInstanceState时,是否有方法捕获应用程序的快照 否则,我必须保存每个布尔值并跟踪它们,尽量不要忘记某些内容,如: @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); if (userAvatar != null) { outState.putString("

调用onSaveInstanceState时,是否有方法捕获应用程序的快照

否则,我必须保存每个布尔值并跟踪它们,尽量不要忘记某些内容,如:

@Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (userAvatar != null) {
            outState.putString("userAvatar", userAvatar.toString());
            avatarAlreadyProvided = true;
        }
        if (sexIsOK) {
            outState.putBoolean("sexIsOK", true);
        }
        if (passwordIsOk) {
            outState.putBoolean("passwordIsOk", true);          
        }
        if (passwordAgainIsOk) {
            outState.putBoolean("passwordAgainIsOk", true);         
        }
        if (passwordsMatch) {
            outState.putBoolean("passwordsMatch", true);                
        }
        if (userHasProvidedOwnPhoto != false) {
            outState.putBoolean("userHasProvidedOwnPhoto", true);
            outState.putBoolean("avatarAlreadyProvided", true);
        }
        if (mImageBitmap != null) {
            outState.putParcelable("mImageBitmap", mImageBitmap);
            avatarAlreadyProvided = true;
        }
        if (usernameIsOk) {
            outState.putBoolean("usernameIsOk", true);          
        }
        if (allSignupFieldsProperlyFilled) {
            outState.putBoolean("allSignupFieldsProperlyFilled", true);         
        }
    }

    @Override
    protected void onRestoreInstanceState(Bundle outState) {
        super.onRestoreInstanceState(outState);
        userHasProvidedOwnPhoto = outState.getBoolean("userHasProvidedOwnPhoto");
        avatarAlreadyProvided = outState.getBoolean("avatarAlreadyProvided");
        mImageBitmap = outState.getParcelable("mImageBitmap");
        userAvatar = outState.getString("userAvatar");
        iUserAvatar.setImageBitmap(mImageBitmap);
        allSignupFieldsProperlyFilled = outState.getBoolean("allSignupFieldsProperlyFilled");
        usernameIsOk = outState.getBoolean("usernameIsOk");
        passwordsMatch = outState.getBoolean("passwordsMatch");
        passwordAgainIsOk = outState.getBoolean("passwordAgainIsOk");
        passwordIsOk = outState.getBoolean("passwordIsOk");
        sexIsOK = outState.getBoolean("sexIsOK");


    }

我总是忘记一些事情,然后把它搞砸。必须有其他方法……

将变量包装到Java类中并使用序列化。您可以使用Gson进行Json序列化


好吧,再一次,我必须得到我所有的变量?你能给我举个例子吗?即使Java对象序列化中有100个变量,也会将保存和加载减少到几行。首先向项目中添加一个新类,并将所有变量移动到其中。然后看看我链接到的另一个答案。那么我在onSaveInstanceState中做什么呢?我序列化变量对象?