尝试使用Java Android Studio永久固定
我正在尝试使用以下代码进行setText,该代码正常工作,但是当我更改活动然后恢复到活动时,SetText似乎重置。还有另一种解决这个问题的方法吗?
public void onClick(View v) {
String Note = editTextNote.getText().toString();
String Email = mAuth.getCurrentUser().getEmail();
String Status = ("IN");
tvClockingStatus.setText("You are clocked: IN");
I am trying to setText using the following code, the code works fine however when I change activity and then revert back to the activity, the setText seems to reset. Is there another method to resolve this?
public void onClick(View v) {
String Note = editTextNote.getText().toString();
String Email = mAuth.getCurrentUser().getEmail();
String Status = ("IN");
tvClockingStatus.setText("You are clocked: IN");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在普通应用程序生命周期中的不同时间创建和破坏了Android中的活动(例如,旋转屏幕或导航时)。如果您希望数据持续时间比活动的生命周期更长,则需要将其存储在更永久的地方。
存储少量数据的一个常见地点是
将键值对写入磁盘上,以便将数据保存,即使应用程序被停止并重新启动,或者活动被破坏并重新创建。sharedpreferences
这是一个简单的示例,说明如何使用
sharedPreferences
保存“时钟”状态字符串。在on Resume
调用数据中,数据是从保存的首选项中加载的,并应用于文本视图。Activities in Android are created and destroyed at various times in the normal application lifecycle (e.g. when you rotate the screen, or navigate away). If you want data to persist longer than the lifecycle of an Activity you need to store it somewhere more permanent.
One common place to store small amounts of data is
SharedPreferences
, which writes key-value pairs to disk so the data is saved even if the app is stopped and restarted or if the activity is destroyed and recreated.Here is a simple example of how you could save a "clocked in" status string using
SharedPreferences
. In theonResume
call the data is loaded from the saved preferences and applied to the TextView.