Android HashMap在返回活动时不会持久 [英] Android HashMap not persisting when returning to activity
问题描述
当我导航到另一个活动并返回时,我试图保留我的HashMap值。这是我现在的代码。
HashMap可以在视图中抓取并保存EditText中的数据。
然而,只要我离开活动并返回,HashMap就会重新初始化为空 - > {}
我看过文档,看起来这是正确的方法确保可变数据持久。然而,它不起作用。
请让我知道可能是什么问题:
public class ScriptActivity extends MainActivity {
HashMap timeAndMessages;
EditText消息;
EditText时间;
@Override
保护无效的onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_script);
if(savedInstanceState!= null){
timeAndMessages =(HashMap)savedInstanceState.getSerializable(alerts);
} else {
timeAndMessages = new HashMap();
}
message =(EditText)findViewById(R.id.messageText);
time =(EditText)findViewById(R.id.timeText);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState){
super.onRestoreInstanceState(savedInstanceState);
restore(savedInstanceState);
}
private void restore(Bundle savedInstanceState){
if(savedInstanceState!= null){
timeAndMessages =(HashMap)savedInstanceState.getSerializable(alerts) ;
$ public void createMessage(View view){
String stringmessage = message.getText()。toString() ;
int inttime = Integer.parseInt(time.getText()。toString());
timeAndMessages.put(inttime,stringmessage);
Toast.makeText(getApplicationContext(),将显示:+ stringmessage +At time:+ Integer.toString(inttime),Toast.LENGTH_LONG).show();
}
@Override
public void onSaveInstanceState(Bundle outState,PersistableBundle outPersistentState){
super.onSaveInstanceState(outState,outPersistentState);
outState.putSerializable(alerts,timeAndMessages);
$ div $解析方案
但是,只要我离开活动并返回,HashMap就会重新初始化为空 - > {}
如果通过离开活动并返回,你的意思是按BACK按钮,然后做一些事情来开始一个新的活动......那么你的行为是预期的。
保存实例状态的 按BACK可以销毁活动,但这两者都不是。如果这 Bundle
用于两种主要情况:
ul>
HashMap
表示模型数据,那么这个状态就不会被保存。 SharedPreferences
,其他类型的文件或云。 您可以阅读有关 活动
文档。
I am trying to keep my HashMap values when I navigate to another activity and return. This is the code I have for now.
The HashMap works and is able to grab and save the data from the EditText in the view.
However as soon as I leave from the activity and return, the HashMap is reinitialized to empty -> {}
I have looked at documentation and it seems this is the correct way of ensuring that a variable data is persisted. However it does not work.
please let me know what could be the issue:
public class ScriptActivity extends MainActivity {
HashMap timeAndMessages;
EditText message;
EditText time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_script);
if (savedInstanceState != null) {
timeAndMessages = (HashMap) savedInstanceState.getSerializable("alerts");
} else {
timeAndMessages = new HashMap();
}
message = (EditText)findViewById(R.id.messageText);
time = (EditText)findViewById(R.id.timeText);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
restore(savedInstanceState);
}
private void restore(Bundle savedInstanceState) {
if (savedInstanceState != null) {
timeAndMessages = (HashMap) savedInstanceState.getSerializable("alerts");
}
}
public void createMessage (View view){
String stringmessage = message.getText().toString();
int inttime = Integer.parseInt(time.getText().toString());
timeAndMessages.put(inttime, stringmessage);
Toast.makeText(getApplicationContext(), "Will display : " + stringmessage + " At time : " + Integer.toString(inttime) , Toast.LENGTH_LONG).show();
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
outState.putSerializable("alerts", timeAndMessages);
}
}
However as soon as I leave from the activity and return, the HashMap is reinitialized to empty -> {}
If by "leave from the activity and return", you mean press the BACK button, then do something to start a fresh activity... then your behavior is expected.
The Bundle
for the saved instance state is used in two main scenarios:
- Configuration changes (e.g., user rotates the screen)
- Process termination, and the user returns to your recent task (e.g., via the overview screen)
Pressing BACK to destroy the activity is neither of those. Hence, the state is not saved.
If this HashMap
represents model data — the sort of data that you expect to be able to get back to, time and again, no matter how the user uses your app — save it to a database, SharedPreferences
, other sort of file, or "the cloud".
You can read more about these scenarios in the Activity
documentation.
这篇关于Android HashMap在返回活动时不会持久的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!