[英]SavedInstanceState is null
I want to get the current system time when I "sync" with another device. 当我与另一台设备“同步”时,我想获取当前系统时间。 So this activity is usually called/displayed right after syncing. 因此,通常在同步后立即调用/显示此活动。
Currently, the current system time is right but I also have alarm pop up another activity and once I'm finished with that activity, I want to go back to this activity. 当前,当前系统时间是正确的,但是我还弹出了另一个警报,该活动结束后,我想返回该活动。 Once I do, the system time is updated to most current time and not the old current time (which is what I want). 完成后,系统时间将更新为大多数当前时间,而不是旧的当前时间(这就是我想要的时间)。
I tried to use SavedInstanceState
to save the state of the old current time and while it does get the right string in the onSavedInstanceState
. 我尝试使用SavedInstanceState
保存旧的当前时间的状态,尽管它确实在onSavedInstanceState
中获得正确的字符串。 When I go back to this activity, the SavedInstanceState
is null. 当我回到此活动时, SavedInstanceState
为null。 I don't know why? 不知道为什么
public class InfoActivity extends BaseActivity {
String fileLastSync = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.info);
Resources resources = context.getResources();
if (savedInstanceState != null) {
// Restore value of members from saved state
fileLastSync = savedInstanceState.getString("FileLastSync");
} else {
//Gets the current DateTime
long nextTime = (System.currentTimeMillis());
Date date = new Date(nextTime);
//Formats the current DateTime
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy HH:mm a");
String formatDate = format.format(date);
//Last sync time is the current formatted date
fileLastSync = formatDate;
}
//Gets the 'last synced' string and sets to datetime of the last sync
String syncString = String.format(resources.getString(R.string.last_sync), fileLastSync);
//Dynamically sets the datetime of the last sync string
TextView lastSyncTextView = ((TextView) findViewById(R.id.last_sync) );
lastSyncTextView.setText(syncString);
}
@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("FileLastSync",fileLastSync);
super.onSaveInstanceState(savedInstanceState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if (savedInstanceState != null) {
// Restore value of members from saved state
fileLastSync = savedInstanceState.getString("FileLastSync");
} else {
//Gets the current DateTime
long nextTime = (System.currentTimeMillis());
Date date = new Date(nextTime);
//Formats the current DateTime
SimpleDateFormat format = new SimpleDateFormat("MM-dd-yyyy hh:mm a");
String formatDate = format.format(date);
//Last sync time is the current formatted date
fileLastSync = formatDate;
}
}
}
EDIT: I tried to put this: super.onSaveInstanceState(savedInstanceState);
编辑:我试图把它放在: super.onSaveInstanceState(savedInstanceState);
at the end of the method but still doesn't work. 在该方法的末尾,但仍然无法正常工作。
Override onRestoreInstanceState(Bundle savedInstanceState)
to restore the date there. 覆盖onRestoreInstanceState(Bundle savedInstanceState)
以在那里恢复日期。
Also, in your onSaveInstanceState
method call 'super.onSaveInstanceState` as the last statement. 同样,在您的onSaveInstanceState
方法中,调用“ super.onSaveInstanceState”作为最后一条语句。
You should not put in the same code from onSave into onRestore. 您不应将相同的代码从onSave放入onRestore。 Copy the complete if-statement from your onCreate method into the onRestore method instead. 而是将完整的if语句从onCreate方法复制到onRestore方法。 And call super.onRestoreInstanceState as the first statement. 并调用super.onRestoreInstanceState作为第一条语句。
Edit: sorry - was intended to be a comment 编辑:对不起-本来是要发表评论
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.