-1

调用 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");


    }

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

4

1 回答 1

2

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

如何序列化对象并将其保存到 Android 中的文件中?

于 2013-11-05T11:35:04.493 回答