当我继续执行其子活动后返回MainActivity时,我正在尝试在MainActivity中保留EditText的内容。 在MainActivity中,我有两个EditText字段,一个存储名字 其他商店的姓氏和按钮,点击时调用newActivity()方法。然后在作为MainActivity的Child的SecondActivity中显示第一个和最后一个名称,但是当我对MainActivity进行bact时,EditText字段的内容不会被保留。
以下是我在MainActivity.java和SecondActivity.java中编写的代码
/************ MainActivity.java *************/
public class MainActivity extends ActionBarActivity {
private EditText firstNameField ;
private EditText lastNameField ;
private String firstName, lastName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstNameField = (EditText) findViewById(R.id.firstNameField);
lastNameField = (EditText) findViewById(R.id.lastNameField);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void newActivity(View view) {
firstName = String.valueOf(firstNameField.getText());
lastName = String.valueOf(lastNameField.getText());
Intent secondActivity = new Intent(this, SecondActivity.class);
secondActivity.putExtra("First name", firstName);
secondActivity.putExtra("Last Name", lastName);
secondActivity.putExtra("Last name", lastName);
startActivity(secondActivity);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
outState.putString(FIRST_NAME_KEY, firstName);
outState.putString(LAST_NAME_KEY, lastName);
super.onSaveInstanceState(outState, outPersistentState);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
// Always call the superclass so it can restore the view hierarchy
super.onRestoreInstanceState(savedInstanceState);
// Restore state members from saved instance
firstName = String.valueOf(savedInstanceState.getString(FIRST_NAME_KEY));
lastName = String.valueOf(savedInstanceState.getString(LAST_NAME_KEY));
firstNameField.setText(""+firstName);
lastNameField.setText(""+lastName);
}
}
/************** SecondActivity.java ***********/
public class SecondActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
String fname = String.valueOf(getIntent().getStringExtra("First name"));
String lname = String.valueOf(getIntent().getStringExtra("Last name"));
EditText firstNameEdt = (EditText) findViewById(R.id.firstNameEdt);
EditText lastNameEdt = (EditText) findViewById(R.id.lastNameEdt);
firstNameEdt.setText(""+ fname);
lastNameEdt.setText("" + lname);
}
}
答案 0 :(得分:0)
您可以在SharedPreferences
中保存edittexts的值。在那里,您可以使用已定义的密钥保存持久值。
您希望保存MainActivity onDestroy
中的值,并在onCreate
中检索您的值。
在这里,您可以阅读如何使用SharedPreferences
:Saving Key-Value Sets
答案 1 :(得分:0)
大多数UI小部件负责在活动实例之间传播自己的状态。如果您注释掉onSaveInstanceState
和onRestoreInstanceState
方法,它可能会起作用。如果您的活动具有与UI分开的自己的状态,则只需要实现这些方法。
答案 2 :(得分:0)
对于一个简单的方法,如果您可以将内容带到第二个活动,为什么不在回到第一个活动时做同样的事情。使用bundle返回第1个活动,然后将其设置为Text