I would like to know how to keep the state of my progressbar or resume where it left off. Let me explain, it's a timer with a progress bar, when the user close the activity or close the activity, the timer is started as a background service (the timer works correctly), I would like that when the user close the activity or close the application, and when he returns, the progress bar will resume where it was. for now what happens is when it returns to the timer, the progress bar returns to zero. here is my code of my adapter where Lessons and Timer service takes place
This how I save progress bar state
private static SharedPreferences.Editor pref;
private static int saveProgressBarState;
private static SharedPreferences getProgressBarState;
private CountDownTimer countDown(final long time, final ProgressBar lessonProgress, long originalTimerSize) {
final int timerLengthInSecond = (int) time / 1_000;
lessonProgress.setMax((int) originalTimerSize / 1_000);
return new CountDownTimer(time, 1000) {
@Override
public void onTick(long l) {
long secondRemaining = l / 1_000;
lessonProgress.setProgress((int) (timerLengthInSecond - secondRemaining));
//Getting progress of progress bar
saveProgressBarState = lessonProgress.getProgress();
}
@Override
public void onFinish() {
lessonProgress.setProgress(timerLengthInSecond);
}
};
}
//this method is called when onPause() is called on the activity
public static void runLessonTimerInBackground(Context context) {
pref = context.getSharedPreferences("LESSONPROGRESS", MODE_PRIVATE).edit();
pref.putInt("savedprogress", progressBarStateSave);
pref.apply();
}
I want to get saved progressbarstate and resume progress where it was and continue progress
You need to add these links in your onResume()
I m assuming you can tweak and apply this in your real-progress of the lesson
int lastProgress = pref.getInt("savedprogress", 0); //0 is default value
lessonProgress.setProgress(lastProgress); //this will set the progress
//here you might also need to do some other stuff related to your code and req
//you can also resume your lesson video from here, etc.
If you want to save multiple lesson progress in sharePref, I suggest you to save progress like
String progressPrefName = "savedProgress_" + lessonID; //here lessonID is lesson's unique ID
pref.putInt(progressPrefName, progressBarStateSave); // here pass the name which can be "savedProgress_1" referring to lesson with ID 1
You can then get it like
String progressPrefName = "savedProgress_" + lessonID;
int lastProgress = pref.getInt(progressPrefName, 0); //0 is default value
lessonProgress.setProgress(lastProgress); //this will set the progress