[英]how to save EditText's value after performing an other activity
i have my first activity with two EditText fields with hint's as first name and last name, when ever i go to my second activity and return to my first activity by a widget button the EditText fields in my first activity gets reset. 我的第一个活动带有两个带有提示提示的EditText字段,分别是名字和姓氏,当我进入第二个活动并通过小部件按钮返回到第一个活动时,第一个活动中的EditText字段将被重置。
SIMPLY i want my EditText fields to remain same in my first activity as i re-visit my first activity from my second activity. 很简单,我希望我的EditText字段在我的第一个活动中保持不变,因为我从第二个活动中重新访问了第一个活动。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButton1Click(View v)
{
Intent intent = new Intent (this,MainActivity2.class);
startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
SOLUTION - i changed my second activity to this. 解决方案-我将第二项活动更改为此。 just added reference to my Button and applied an onClickListener with onBackPressed(); 刚刚添加了对我的Button的引用,并通过onBackPressed()应用了onClickListener; code. 码。
setContentView(R.layout.activity_main2);
Button button = (Button)findViewById(R.id.button3); //gave reference of button in second activity
button.setOnClickListener(new View.OnClickListener() { //and applied an onClickListener with code onBackpressed();
public void onClick(View v) {
onBackPressed();
// Code here executes on main thread after user presses button
}
});
}
Really Really thnx to @Narendra Sorathiya for his guidance. 非常感谢@Narendra Sorathiya的指导。
Open secondActivity like below, 如下所示打开secondActivity,
Intent i = new Intent(A.this,B.class);
startActivity(i);
finish();
to 至
Intent i = new Intent(A.this,B.class);
startActivityForResult(i,1);
do not open 1st Activity from 2nd Activity, just need to back press. 不要从第二活动中打开第一活动,只需要向后按即可。
try this declare all variavle at top of activity 试试这个在活动顶部声明所有变量
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String FNAME = "fname";
public static final String LNAME = "lname";
SharedPreferences sharedpreferences;
write this code when you move to your next activity 转到下一个活动时编写此代码
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
// store login data in sharedpreferences
SharedPreferences.Editor editor = sharedpreferences.edit();
editor.putString(FNAME, edittext.getText());
editor.putString(LNAME, edittext2.getText());
editor.commit(); //save data in sharedpreferences
write this code in your oncreate method after you bind your controls 绑定控件后,在您的oncreate方法中编写此代码
and get data from sharedprefrence like this 并像这样从sharedprefrence获取数据
String fname= prefs.getString(FNAME, "");
String fname= prefs.getString(LNAME, "");
than set it to your editext like this 而不是像这样将其设置为您的editext
editext.setText(fname);
editext2.setText(lname);
let me know in case of any query 如有任何疑问,请通知我
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.