I'm storing textview values in sharedpreferences and im not sure to load them in oncreate or in onresume?
I want to show these values again when the user closes the app and then opens it again.
StorageUtil is a class that manages my sharedpreferences.
I want to store the seekbar value in sharedpreferences.
Seekbar value
mSeekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
StorageUtil storageUtil = new StorageUtil(getApplicationContext());
if (mediaPlayer != null && fromUser){
lastSeekbarPos = progress;
mSeekbarProgress = progress * 1000;
mediaPlayer.seekTo(mSeekbarProgress);
}
//Store last SeekBar position in mSharedPreferences
storageUtil.storeSeekbarProgress(lastSeekbarPos);
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
songList = storageUtil.getSongs();
songIndex = storageUtil.loadSongIndex();
lastSeekbarPos = storageUtil.loadSeekbarProgress();
if (songList != null && !songList.isEmpty() && songIndex != -1){
lastSeekbarPos = storageUtil.loadSeekbarProgress();
tvSongTitle.setText(songList.get(songIndex).getTitle());
tvArtistName.setText(songList.get(songIndex).getArtist());
mSeekbar.setProgress(lastSeekbarPos);
loadAlbumArt();
loadAlbumArtBottom();
Log.i(TAG, "Data found in mSharedPreferences");
}else{
mSlideUpPanel.setPanelState(SlidingUpPanelLayout.PanelState.HIDDEN);
tvSongTitle.setText("");
tvArtistName.setText("");
}
StorageUtil
public class StorageUtil {
private final String STORAGE = "com.vince_mp3player.STORAGE";
private SharedPreferences mSharedPreferences;
private Context context;
private SharedPreferences.Editor mEditor;
public StorageUtil(Context context){
this.context = context;
}
public void storeSeekbarProgress(int seekbarprogress) {
mSharedPreferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
mEditor = mSharedPreferences.edit();
mEditor.putInt("seekbarProgress", seekbarprogress);
mEditor.apply();
}
public int loadSeekbarProgress() {
mSharedPreferences = context.getSharedPreferences(STORAGE, Context.MODE_PRIVATE);
return mSharedPreferences.getInt("seekbarProgress", -1);//return -1 if no data found
}
}
Either should work.
onCreate()
is called if the Activity fully finishes, ie you close the app from Recents.
onResume()
is also called right after onCreate()
, as well as any time the Activity re-enters the foreground, ie going home and then opening the app from Recents.