How to retain value in second activity after re-clicking the button from MainActivity to launch the second activity?

huangapple go评论53阅读模式
英文:

How to retain value in second activity after re-clicking the button from MainActivity to launch the second activity?

问题

我有2个活动。第一个是MainActivity,第二个是SecondActivity

MainActivity有一个名为"Launch"的textViewbutton。使用intentstartActivityForResult,我将一个列表传递给SecondActivity

SecondActivity有一个spinner和2个button:一个选择button和一个取消button

在从spinner中选择项目后,当用户单击Select按钮时,MainActivitytextView中显示了在spinner中选择的选项。

现在,当用户再次点击"Launch"时,SecondActivity会启动,但spinner不会保留用户上次选择的值,而是显示spinner的第一个值。

有没有办法让用户在上一次使用SecondActivity时选择的值保留在SecondActivity中?

英文:

I have 2 activities. First is MainActivity and second is SecondActivity.

MainActivity has a textView and buttoncalled "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.

huangapple
  • 本文由 发表于 2020年7月31日 00:49:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63177783.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定