Android Studio:如何保存上一Activity选中的数据?
我来帮你梳理下这个问题的可能原因和解决思路哈!
核心问题分析
你的流程是:Game_1000页面中用TextView(cpu1000)作为按钮,点击跳转至cpu_g1000页面选择CPU单选按钮,选中后将字符串传回Game_1000替换默认的“Choose a processor”文本,但切换到主板选择时,处理器相关数据出现异常。结合常见的Android开发场景,大概率是状态保存、数据传递时机或页面生命周期出了问题,下面给你具体的排查和修复方案:
1. 先排查数据传递与状态持久化问题
如果只是用Intent临时传值,当Game_1000页面因切换到主板页面被系统后台回收时,之前选中的CPU值会丢失,导致返回后文本重置为默认值。建议用持久化存储+生命周期回调来保证数据不丢失:
方案示例:用SharedPreferences保存选中状态
- 在cpu_g1000页面选中单选按钮后,立即保存值:
// 假设点击确认按钮完成选择 confirmBtn.setOnClickListener(v -> { RadioGroup cpuGroup = findViewById(R.id.cpu_radio_group); int selectedId = cpuGroup.getCheckedRadioButtonId(); if (selectedId != -1) { RadioButton selectedRb = findViewById(selectedId); String selectedCpu = selectedRb.getText().toString(); // 保存到SharedPreferences SharedPreferences sp = getSharedPreferences("hardware_choices", MODE_PRIVATE); sp.edit().putString("selected_cpu", selectedCpu).apply(); // 同时返回数据给Game_1000 Intent resultIntent = new Intent(); resultIntent.putExtra("SELECTED_CPU", selectedCpu); setResult(RESULT_OK, resultIntent); finish(); } });
- 在Game_1000的
onResume方法中读取并更新文本(每次回到页面都刷新,避免状态丢失):
@Override protected void onResume() { super.onResume(); SharedPreferences sp = getSharedPreferences("hardware_choices", MODE_PRIVATE); String savedCpu = sp.getString("selected_cpu", "Choose a processor"); cpu1000.setText(savedCpu); }
2. 检查页面切换时的生命周期问题
如果你的主板选择页面是通过startActivity启动的,Game_1000可能因内存不足被销毁,再次返回时会重新执行onCreate方法。如果你的onCreate里硬编码设置了TextView的默认文本,就会覆盖之前的选中值:
// 错误示例:每次创建都重置文本 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game_1000); cpu1000 = findViewById(R.id.cpu1000); cpu1000.setText("Choose a processor"); // 这里会覆盖保存的选中值 }
修复方式:
只有在首次创建页面时才设置默认文本,否则从SharedPreferences读取:
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_game_1000); cpu1000 = findViewById(R.id.cpu1000); SharedPreferences sp = getSharedPreferences("hardware_choices", MODE_PRIVATE); String savedCpu = sp.getString("selected_cpu", null); if (savedCpu == null) { cpu1000.setText("Choose a processor"); } else { cpu1000.setText(savedCpu); } }
3. 确认数据传递的正确性(推荐用Activity Result API)
如果你还在使用旧的startActivityForResult,建议换成更稳定的Activity Result API,避免因回调时机问题导致数据未更新:
- 在Game_1000中注册结果回调:
private ActivityResultLauncher<Intent> cpuLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> { if (result.getResultCode() == RESULT_OK && result.getData() != null) { String selectedCpu = result.getData().getStringExtra("SELECTED_CPU"); cpu1000.setText(selectedCpu); // 同步保存到SharedPreferences SharedPreferences sp = getSharedPreferences("hardware_choices", MODE_PRIVATE); sp.edit().putString("selected_cpu", selectedCpu).apply(); } } );
- 点击TextView时启动cpu_g1000:
cpu1000.setOnClickListener(v -> { Intent intent = new Intent(Game_1000.this, cpu_g1000.class); cpuLauncher.launch(intent); });
4. 如果是主板与CPU的联动异常
如果问题是“选完主板后,之前选的CPU值出现错误(比如消失、变成默认值)”,还要检查主板选择页面的逻辑:
- 是否在主板选择时不小心重置了SharedPreferences里的CPU值?
- 是否主板选择后需要更新CPU的可选范围,但联动逻辑出错导致显示异常?
可以在主板选择页面的保存逻辑里,单独处理主板值,不要触碰CPU的存储键:
// 主板选择页面的保存逻辑 SharedPreferences sp = getSharedPreferences("hardware_choices", MODE_PRIVATE); sp.edit().putString("selected_motherboard", selectedMotherboard).apply();
内容的提问来源于stack exchange,提问作者Tashi Tamu




