Android studio – 如何保存以前活动中选择的数据

这是我的代码片段。 Textview充当按钮并在Onclicklistner上具有Onclicklistner 。 当单击cpu1000 Textview ,它将导致cpu_g1000类,其代码如下所示。

 public class Game_1000 extends AppCompatActivity implements View.OnClickListener{ private TextView cpu1000, mobo1000; TextView cpu, mobo; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game_1000); cpu1000 = (TextView) findViewById(R.id.proName_G1); mobo1000 = (TextView) findViewById(R.id.moboName_G1); cpu1000.setOnClickListener(this); mobo1000.setOnClickListener(this); cpu = (TextView) findViewById(R.id.proNameG1000); cpu.setText(getIntent().getStringExtra("Processor")); mobo = (TextView) findViewById(R.id.moboNameG1000); mobo.setText(getIntent().getStringExtra("Motherboard")); } @Override public void onClick(View v) { if (v == cpu1000) { opencpu_g1000(); } else if (v == mobo1000) { openmobo_g1000(); } } public void opencpu_g1000() { Intent intent = new Intent(this, cpu_g1000.class); startActivity(intent); } public void openmobo_g1000() { Intent intent = new Intent(this, mobo_g1000.class); startActivity(intent); } 

在这个课程中,有单选按钮。 用户选择其中一个选项,选择更改为字符串。 字符串被发送回Game_1000类。 然后该字符串将替换为“选择处理器”以显示新选项。 我遇到的问题是,当我选择主板时,处理器选择将恢复为“选择处理器”并且主板选择显示。 我需要两个同时展示。

 public class cpu_g1000 extends AppCompatActivity { Button button_save; RadioGroup rG; TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_cpu_g1000); button_save = (Button) findViewById(R.id.Save_G1_cpu); rG = (RadioGroup) findViewById(R.id.cpu_RadioGrp); tv = (TextView) findViewById(R.id.proNameG1000); button_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selected_cpu = rG.getCheckedRadioButtonId(); RadioButton selectedRadioButton = (RadioButton) findViewById(selected_cpu); String radioValue = selectedRadioButton.getText().toString(); Intent intent = new Intent(cpu_g1000.this, Game_1000.class); intent.putExtra("Processor", radioValue); startActivity(intent); } }); } 

选择处理器:

处理器被选中

选择主板:

主板被选中

选择示例:

选择示例

之前的选择不会被记录,当做出新的选择时,之前的选择将恢复为“请选择处理器”,但我需要显示这两个信息。 在我的原始代码中,我有两个以上的选择,但我缩短了它以便于阅读。

通过调用startActivityForResult(...)开始第二个(Selection)活动,然后当用户完成交互时,在Intent设置所选数据并将该意图传递给具有所需结果setResult(...)方法,然后调用finish第二项活动。

完成第二个活动后,您将在第一个活动的onActivityResult(...)方法中接收意图数据,从意图中提取该数据,然后将其显示给用户。

更多描述

尝试使用共享首选项 。 这是一个很好的教程 。

对于您的场景,您需要做的是保存处理器和主板选择结果或将数据从一个活动发送到另一个活动。 在处理器选择之后,您将返回Game_1000 Activity,您可以使用startActivityForResult 。 此方法将结果发送回通过Intent调用Activity。 这意味着如果您从Game_1000活动中调用cpu_g1000活动,并且在选择处理器后,您可以将结果发送回Game_1000活动。 Game_1000 Activity将在onActivityResult方法中获得结果。

例:

使用来自Game_1000 Activity的startActivityForResult启动Game_1000活动。

 public void opencpu_g1000() { Intent intent = new Intent(this, cpu_g1000.class); startActivityForResult(intent, PROCESSOR_SELECTION_CODE); } 

cpu_g1000 Activity中将结果设置为intent并发送回Game_1000 Activity

  button_save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int selected_cpu = rG.getCheckedRadioButtonId(); RadioButton selectedRadioButton = (RadioButton) findViewById(selected_cpu); String radioValue = selectedRadioButton.getText().toString(); Intent resultIntent = new Intent(); // TODO Add data to send back resultIntent.putExtra("ProcessorSelection", radioValue); setResult(Activity.RESULT_OK, resultIntent); // finishing this activity finish(); } }); 

Game_1000 Activity中,通过覆盖onActivityResult方法获取从Game_1000 Activity发送的值。

 @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch(requestCode) { case (PROCESSOR_SELECTION_CODE) : { if (resultCode == Activity.RESULT_OK) { // TODO Extract the data returned from the child Activity. String radioValue = data.getStringExtra("ProcessorSelection"); // save the result value and update UI } break; } } } 

对于mobo_g1000 Activity,使用相同的过程将结果返回Game_1000 Activity。 使用startActivityForResult启动mobo_g1000 Activity并获取Game_1000 Activity的onActivityResult方法中的值。 您必须在mobo_g1000活动的onActivityResult方法中添加另一个case

检查此链接以及此链接以更好地了解此工作的方式。