I've been experimenting for a couple of hours now on how to save the progress of a progressBar even if I closed/destroyed the app. I tried using the same method in TextViews and it works just fine. So, I'm wondering where I went wrong. Below is my code that I've done and any response is greatly appreciated! Have a nice day!
public class MainActivity extends AppCompatActivity {
public static final String PROGRESS = "progress";
private int CurrentProgress;
private ProgressBar progressBar;
private Button addProgress;
public static final String SHARED_PREFS="sharedPrefs";
private int getCurrentProgress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
addProgress = findViewById(R.id.addProgress);
progressBar.setMax(1000);
addProgress.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CurrentProgress = CurrentProgress + 100;
progressBar.setProgress(CurrentProgress);
saveData();
}
});
loadData();
updateData();
}
public void saveData(){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(PROGRESS, CurrentProgress);
editor.apply();
}
public void loadData() {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
getCurrentProgress = sharedPreferences.getInt(PROGRESS, CurrentProgress);
}
public void updateData() {
progressBar.setProgress(CurrentProgress);
}
}
There are a few problems with your logic and syntax. Logic first:
First, when you're setting the value of getCurrentProgress
in the loadData
method, the second argument of .getInt
is the default value that you want to set getCurrentProgress
to. Right now, you're saying that if PROGRESS
is not found in sharedPrefs, then the getCurrentProgress
will be set to CurrentProgress
, which is empty. Just change it to whatever you want the default value to be, probably zero or one:
getCurrentProgress = sharedPreferences.getInt(PROGRESS, 0);
Second, in your updateData
method, you're setting progressBar
's progress to CurrentProgress
, which is, again, empty. You should be setting it to getCurrentProgress
.
For syntax: your choice of variable names is very confusing. You should not name an int
getCurrentProgress
, that is the name of a method. You should not capitalize a variable name, CurrentProgress
looks like a class. Also, make sure your indentations line up properly - the calls to loadData
and updateData
in onCreate
are indented too far and it is difficult to read.
Also, I recommend adding more descriptive names to your variables, eg addProgress
might be better as addProgressBtn
. This helps you keep track of things better once you have a more complicated layout.
Here is what the code should look like:
public class MainActivity extends AppCompatActivity {
public static final String PROGRESS = "progress";
private int currentProgress;
private ProgressBar progressBar;
private Button addProgressBtn;
public static final String SHARED_PREFS="sharedPrefs";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = findViewById(R.id.progressBar);
addProgressBtn = findViewById(R.id.addProgress);
progressBar.setMax(1000);
addProgressBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
currentProgress = currentProgress + 100; // currentProgress += 100 works too
progressBar.setProgress(currentProgress);
saveData();
}
});
loadData();
updateData();
}
public void saveData(){
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt(PROGRESS, currentProgress);
editor.apply();
}
public void loadData() {
SharedPreferences sharedPreferences = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
currentProgress = sharedPreferences.getInt(PROGRESS, 0);
}
public void updateData() {
progressBar.setProgress(currentProgress);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.