我有两个活动,第一个是 LoginActivity,其中有五个静态变量:
public static Login loginTargetApp;
public static Gateway gateway;
public static Login loginGateway;
public static OfflineUser offlineUser;
public static boolean isLoggedIn;
在我的第二个活动 MainViewActivity 中,当我在 android 销毁并重新创建我的活动后尝试访问此变量时,它返回 null 并且应用程序崩溃:
jsonTempDownload = new File(Constants.DEST_PATH_PARENT + File.separator
+ LoginActivity.gateway.getUuidInstituicao() + Constants.JSON_TEMP_DOWNLOAD);
我正在尝试保存变量状态:
@Override
protected void onSaveInstanceState(Bundle saveInstanceState) {
saveInstanceState.putParcelable("loginTargetApp", LoginActivity.loginTargetApp);
saveInstanceState.putParcelable("gateway", LoginActivity.gateway);
saveInstanceState.putParcelable("loginGateway", LoginActivity.loginGateway);
saveInstanceState.putParcelable("offlineUser", LoginActivity.offlineUser);
saveInstanceState.putBoolean("isLoggedIn", LoginActivity.isLoggedIn);
super.onSaveInstanceState(saveInstanceState);
}
为了恢复我的变量值,我尝试了两种不同的方法,但没有奏效:
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
LoginActivity.loginTargetApp = savedInstanceState.getParcelable("loginTargetApp");
LoginActivity.gateway = savedInstanceState.getParcelable("gateway");
LoginActivity.loginGateway = savedInstanceState.getParcelable("loginGateway");
LoginActivity.offlineUser = savedInstanceState.getParcelable("offlineUser");
LoginActivity.isLoggedIn = savedInstanceState.getBoolean("isLoggedIn");
}
和:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(Constants.MAIN_VIEW_ACTIVITY, "----------------");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "Abrindo MainViewActivity...");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "----------------");
if (savedInstanceState != null) {
// Restore value of members from saved state
LoginActivity.isLoggedIn = savedInstanceState.getBoolean("isLoggedIn");
boolean isLoggedIn = savedInstanceState.getBoolean("isLoggedIn");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL NORMAL: " + isLoggedIn);
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL ESTÁTICA: " + LoginActivity.isLoggedIn);
LoginActivity.loginTargetApp = savedInstanceState.getParcelable("loginTargetApp");
Login loginTargetApp = savedInstanceState.getParcelable("loginTargetApp");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL NORMAL: " + loginTargetApp.getEmailAndress());
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL ESTÁTICA: " + LoginActivity.loginTargetApp.getEmailAndress());
LoginActivity.gateway = savedInstanceState.getParcelable("gateway");
LoginActivity.loginGateway = savedInstanceState.getParcelable("loginGateway");
LoginActivity.offlineUser = savedInstanceState.getParcelable("offlineUser");
OfflineUser offlineUser = savedInstanceState.getParcelable("offlineUser");
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL NORMAL: " + offlineUser.getEmail());
Log.d(Constants.MAIN_VIEW_ACTIVITY, "VARIÁVEL ESTÁTICA: " + LoginActivity.offlineUser.getEmail());
} else {
Log.d(Constants.MAIN_VIEW_ACTIVITY, "SAVED INSTANCE STATE IS NULL");
}
在 onCreate 方法中,我的 savedInstance 不为空,但是当我尝试在 Log.d 中显示值时,它仍然崩溃。