英文:
How to retain value in second activity after re-clicking the button from MainActivity to launch the second activity?
问题
我有2个活动。第一个是MainActivity
,第二个是SecondActivity
。
MainActivity
有一个名为"Launch"的textView
和button
。使用intent
和startActivityForResult
,我将一个列表传递给SecondActivity
。
SecondActivity
有一个spinner
和2个button
:一个选择button
和一个取消button
。
在从spinner
中选择项目后,当用户单击Select
按钮时,MainActivity
的textView
中显示了在spinner
中选择的选项。
现在,当用户再次点击"Launch"时,SecondActivity
会启动,但spinner
不会保留用户上次选择的值,而是显示spinner
的第一个值。
有没有办法让用户在上一次使用SecondActivity
时选择的值保留在SecondActivity
中?
英文:
I have 2 activities. First is MainActivity
and second is SecondActivity
.
MainActivity
has a textView
and button
called "Launch". Using intent
and startActivityForResult I am passing a list to SecondActivity
.
SecondActivity
has a spinner
and 2 buttons
;a select button
and a cancel
button.
On selection of an item from the spinner
, when user clicks on Select
button, the option chosen in the spinner
is populated in the textView
of MainActivity
.
Now when the user again clicks on "Launch", SecondActivity
launches however, the spinner
doesn't hold the value the user had chosen last time, instead it shows the first value of the spinner
.
Is their anyway, how I can retain the value selected in the SecondActivity
chosen by the user in it's previous use.
答案1
得分: 0
你可以使用SharedPreferences来持久化信息,只需存储用户的最后选择,然后检索它以设置在SecondActivity上。
Context context = getActivity();
SharedPreferences prefs = context.getSharedPreferences(
getString("myPreferences"), Context.MODE_PRIVATE);
//存储一个值
SharedPreferences.Editor editor = prefs.edit();
editor.putString("selection", "foo");
//获取该值
String selection = prefs.getString("selection", "defaultOption");
这是官方文档链接:https://developer.android.com/training/data-storage/shared-preferences
英文:
You can persist information using SharedPreferences, just store the last selection from the user and then retrieve it to set on SecondActivity.
Context context = getActivity();
SharedPreferences prefs = context.getSharedPreferences(
getString("myPreferences"), Context.MODE_PRIVATE);
//Store a value
SharedPreferences.Editor editor = prefs.edit();
editor.putString("selection","foo");
//Get the value
String selection = prefs.getString("selection", "defaultOption");
Here is the official documentation: https://developer.android.com/training/data-storage/shared-preferences
答案2
得分: 0
当您关闭SecondActivity时,应将spinner的选定项目返回到MainActivity。如果是MainActivity,可以在此函数中获取该值:
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SECOND_CODE && resultCode == Activity.RESULT_OK && data != null) {
// 在这里可以获取所选的spinner项目
}
}
在此之前,您应该使用以下代码显示SecondActivity:
private static final int SECOND_CODE = 1001;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, SECOND_CODE);
然后,当您想要再次显示SecondActivity时:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.putExtra("selected_spinner", /*上面函数中获取的spinner项目*/);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, SECOND_CODE);
在SecondActivity中,您可以从getIntent()中获取所选的spinner项目:
String selected_spinner = getIntent().getStringExtra("selected_spinner");
然后,您可以在Spinner中找到此字符串并将其选中。
英文:
When you close the SecondActivity, you should return the select item of spinner to MainActivity. If it is MainActiviy can get that value in this function :
onActivityResult(int requestCode, int resultCode, @Nullable Intent data)
before that, you shold show SecondActivity with
startActivityForResult(intent, SECOND_CODE);
so the code can be like this
private static SECOND_CODE = 1001;
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, SECOND_CODE);
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == SECOND_CODE && resultCode ==
Activity.RESULT_OK && data != null) {
// you can get selected spinner item here
}
}
After that, when you want to show SecondActivity again
Intent intent = new Intent(MainActivity.thisSecondActivity.class);
intent.putExtra("selected_spinner", /*the spinner item you got from above function*/)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivityForResult(intent, SECOND_CODE);
and in SecondActivity, you can get the selected spinner item from getIntent()
String selected_spinner = getIntent().getStringExtra("selected_spinner");
Then you can find this string in Spinner and select it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论