Android Studio:保存水平进度条状态并在Fragment与Activity间传递
现有代码片段
MainActivity(无ProgressBar)
@Override public void onClick(View view) { switch (view.getId()){ case R.id.submitBttn: if (questionTwo.getText().toString().isEmpty() & questionFour.getText().toString().isEmpty() & questionFive.getText().toString().isEmpty()){ questionTwo.setError("You haven't completed this question!"); questionFour.setError("You haven't completed this question!"); questionFive.setError("You haven't completed this question!"); }else{ if (questionOne.isChecked() & questionThree.isChecked()){ startActivity(new Intent(getApplicationContext(),HomeActivity.class)); Toast.makeText(this, "Received Report", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "The question is not fully answered.", Toast.LENGTH_SHORT).show(); } } } }
Fragment(含ProgressBar)
progressBar = view.findViewById(R.id.progressBar); startProgress = view.findViewById(R.id.addProgress); startProgress.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(getActivity(),ReportQuestion.class)); } });
需求说明
起始页面是带有水平进度条的Fragment,点击按钮跳转到无进度条的MainActivity。需实现:
- 保存水平进度条的状态
- 点击MainActivity中的按钮时增加进度条进度
- 实现进度条状态在Fragment与Activity之间的传递
实现方案
方案一:用SharedPreferences持久化进度(适合重启APP后保留进度)
1. Fragment中初始化并读取进度
修改Fragment的onViewCreated方法,读取已保存的进度值:
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); progressBar = view.findViewById(R.id.progressBar); startProgress = view.findViewById(R.id.addProgress); // 读取本地保存的进度,默认0 SharedPreferences sp = requireActivity().getSharedPreferences("ProgressPrefs", Context.MODE_PRIVATE); int savedProgress = sp.getInt("current_progress", 0); progressBar.setProgress(savedProgress); startProgress.setOnClickListener(v -> { startActivity(new Intent(getActivity(), MainActivity.class)); }); }
2. MainActivity中更新并保存进度
修改MainActivity的点击事件,在提交成功后更新进度:
@Override public void onClick(View view) { switch (view.getId()){ case R.id.submitBttn: if (questionTwo.getText().toString().isEmpty() && questionFour.getText().toString().isEmpty() && questionFive.getText().toString().isEmpty()){ questionTwo.setError("你还没完成这道题!"); questionFour.setError("你还没完成这道题!"); questionFive.setError("你还没完成这道题!"); }else{ if (questionOne.isChecked() && questionThree.isChecked()){ // 更新进度,每次加20(可根据需求调整) SharedPreferences sp = getSharedPreferences("ProgressPrefs", MODE_PRIVATE); int currentProgress = sp.getInt("current_progress", 0); int newProgress = Math.min(currentProgress + 20, 100); // 进度不超过100 sp.edit().putInt("current_progress", newProgress).apply(); startActivity(new Intent(getApplicationContext(), HomeActivity.class)); Toast.makeText(this, "已收到报告", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "题目未完全作答。", Toast.LENGTH_SHORT).show(); } } } }
3. 返回Fragment时刷新进度
在Fragment的onResume方法中重新读取进度,确保返回时显示最新状态:
@Override public void onResume() { super.onResume(); SharedPreferences sp = requireActivity().getSharedPreferences("ProgressPrefs", Context.MODE_PRIVATE); int savedProgress = sp.getInt("current_progress", 0); progressBar.setProgress(savedProgress); }
方案二:用ViewModel共享实时进度(适合临时状态同步,无需持久化)
如果不需要重启APP保留进度,可通过ViewModel实现组件间实时状态共享:
1. 创建共享ViewModel
public class ProgressViewModel extends ViewModel { private MutableLiveData<Integer> progressLiveData = new MutableLiveData<>(0); // 设置进度,确保不超过100 public void setProgress(int progress) { progressLiveData.setValue(Math.min(progress, 100)); } // 增加指定进度值 public void addProgress(int addValue) { int current = progressLiveData.getValue() != null ? progressLiveData.getValue() : 0; setProgress(current + addValue); } // 提供进度观察入口 public LiveData<Integer> getProgressLiveData() { return progressLiveData; } }
2. Fragment中观察进度变化
@Override public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); progressBar = view.findViewById(R.id.progressBar); startProgress = view.findViewById(R.id.addProgress); ProgressViewModel viewModel = new ViewModelProvider(requireActivity()).get(ProgressViewModel.class); // 观察进度变化,实时更新进度条 viewModel.getProgressLiveData().observe(getViewLifecycleOwner(), progress -> { progressBar.setProgress(progress); }); startProgress.setOnClickListener(v -> { startActivity(new Intent(getActivity(), MainActivity.class)); }); }
3. MainActivity中更新进度
@Override public void onClick(View view) { switch (view.getId()){ case R.id.submitBttn: if (questionTwo.getText().toString().isEmpty() && questionFour.getText().toString().isEmpty() && questionFive.getText().toString().isEmpty()){ questionTwo.setError("你还没完成这道题!"); questionFour.setError("你还没完成这道题!"); questionFive.setError("你还没完成这道题!"); }else{ if (questionOne.isChecked() && questionThree.isChecked()){ ProgressViewModel viewModel = new ViewModelProvider(this).get(ProgressViewModel.class); viewModel.addProgress(20); // 增加进度 startActivity(new Intent(getApplicationContext(), HomeActivity.class)); Toast.makeText(this, "已收到报告", Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(this, "题目未完全作答。", Toast.LENGTH_SHORT).show(); } } } }
补充说明
- 若需要重启APP后仍保留进度,可结合两种方案:在ViewModel初始化时读取SharedPreferences的进度值,更新时同时保存到SharedPreferences。
- 进度增量可根据实际业务调整,比如按任务完成比例计算。
内容的提问来源于stack exchange,提问作者Mohamed Danysh Rozaily
相关产品推荐
相关产品推荐

