- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在与另一台设备“同步”时获取当前系统时间。因此,此 Activity 通常在同步后立即调用/显示。
目前,当前系统时间是正确的,但我还弹出另一个 Activity 的警报,一旦我完成该 Activity ,我想返回到该 Activity 。一旦我这样做,系统时间将更新为最新时间,而不是旧的当前时间(这就是我想要的)。
我尝试使用SavedInstanceState
来保存旧当前时间的状态,并且它确实在onSavedInstanceState
中获取了正确的字符串。当我返回此 Activity 时,SavedInstanceState
为 null。我不知道为什么?
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;
}
}
}
编辑:我尝试将以下内容: super.onSaveInstanceState(savedInstanceState);
放在方法的末尾,但仍然不起作用。
最佳答案
您不应将 onSave 中的相同代码放入 onRestore 中。将完整的 if 语句从 onCreate 方法复制到 onRestore 方法中。并调用 super.onRestoreInstanceState 作为第一条语句。
编辑:抱歉 - 本来是一条评论
关于java - SavedInstanceState 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26939420/
相当简单的问题,但我无法在任何地方找到明确的答案。 fragment 的savedInstanceState和 fragment 的父activity(或封装 fragment )的savedInst
我在获取 Activity 状态时遇到问题。我有 2 个 Activity :MainActivity 和 Activity2。在 MainActivity 中,我放了一个 editText 和一个
我想在与另一台设备“同步”时获取当前系统时间。因此,此 Activity 通常在同步后立即调用/显示。 目前,当前系统时间是正确的,但我还弹出另一个 Activity 的警报,一旦我完成该 Activ
在 Google Api 示例代码中,他们有一个关于保存状态的非常简单的示例。我需要这个来处理屏幕旋转。 我的问题是我不明白方法 getSavedText() 和 setSaveText() 是从哪里
我有一个秒表应用程序,它只有一项 Activity 。三个按钮启动、停止、重置和一个显示计时器的 TextView 。这是我的代码: public class StopwatchActivity ex
我有一个 ViewPager,其中包含用户输入不同输入的一些 fragment 。其中一个 fragment 包含一个 DatePicker 和一个 TimePicker ,我将其存储(当用户选择了日
如果我没有显式调用 onSaveInstanceState(),当 onCreate 中的 savedInstanceState 不为 null 时,这是可能的吗? 我看到一个选项,当系统在崩溃后重新
所以我的 OnsavedInstanceState 中有这段代码 @Override public void onSaveInstanceState(Bundle savedInstanceState
我正在制作一个 Android 应用程序,但我一直遇到一个奇怪的错误。游戏结束后,如果您点击手机上的“返回”按钮,您将回到完成的游戏并继续玩!! 如何防止这种情况发生?有没有办法删除之前的SavedI
我刚开始使用 Android,我遇到了以下问题: 我正在尝试在打开新 Activity 时保存 InstanceState,并且我需要在 Activity 返回可见状态时恢复该状态。 这是我的 onS
在我的主要 Activity 中有一些代码说 if (savedInstanceState != null) { mCurrentSelectedPosition = save
我是 Android 的新手,我在使用 savedInstanceState 和管理生命周期时遇到了一些麻烦。 http://developer.android.com/guide/topics/fu
我有以下情况: Activity A 启动 Activity B Activity A 调用 onPause、onSaveInstanceState 和 onStop 方法。 在 Activity B
据我了解,Activity 会自动保存 View 的状态,而 EditText 和 TextView 之类的东西会自动恢复它们的状态。 这不适用于可见性吗? 例如:如果默认情况下在 XML 中,我有一
我是 Android 开发的新手,但我对 OOP 概念有一些了解。我正在尝试从 Java 的角度学习 Android。 我了解到第9行onCreate()里面的savedInstanceState是B
因此,Activity.onCreate() 将一个Bundle 作为参数,默认情况下它被称为savedInstanceState。 savedInstanceState 是否在应用更新到较新版本时保
这是我保存的InstaceState 代码: @Override public void onSaveInstanceState(Bundle savedInstanceState) { sa
我在 ViewPager 中有一个 fragment 。 Fragment 包含一个 RecyclerView(想想 ListView)。当用户离开然后返回到 fragment 时,无论用户离开列表的
我只是想通过这个简单的例子来了解 Activity 的生命周期,方法是找出如何取回 SomeIntegers g_values 对象和其中的 ArrayList AnInteger 对象。 事实上,它
我正在使用 onSaveInstanceState 方法来存储和检索 fragment 内的局部变量。我的一个 fragment 中有以下代码(简化): private final SimpleDat
我是一名优秀的程序员,十分优秀!