我将textview值存储在sharedpreferences中,不确定是否要在oncreate或onresume中加载它们?
我想在用户关闭应用程序然后再次打开它时再次显示这些值。
StorageUtil是管理我的共享首选项的类。
我想将seekbar值存储在sharedpreferences中。
搜索栏值
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
}
}
答案 0 :(得分:1)
应该都可以。
如果活动完全结束,即从“最近”关闭应用程序,则会调用 onCreate()
。
onResume()
也紧随onCreate()
之后以及活动重新进入前台(即回家然后从“最近的用户”打开应用程序)的任何时间被调用。