[英]How can i save my seekbar position after closing the app process?
我试图在关闭进程后保存SeekBar
的位置,不仅在将应用程序置于后台时,甚至在我终止进程时也是如此。 我该怎么做?
一种方法是使用共享首选项:
SharedPreference sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor e = sp.edit();
int seekbar = seekbar.getProgress();
e.putInt("BarPosition", seekbar).commit();
然后,每当您加载视图时,都会从共享首选项中获取视图:
int sp = mSharedPrefs.getInt("BarPosition", 0);
seekbar.setProgress(sp);
当应用程序处于后台/已onPause()
时,可以在onPause()
方法上调用它们。
我将使用SharedPreferences
库。 例如:
// If you are inside an activity:
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
// == OR == If you have a context reference:
SharedPreferences sharedPref = context.getSharedPreferences("seekbarPrefs", Context.MODE_PRIVATE);
// To save your value:
int curPos = 25;
sharedPref.edit().putInt("seekbarPosition", curPos).apply();
// To get your value:
int pos = sharedPref.getInt("seekbarPosition", 0);
此值将保留在应用程序的缓存中,直到它们被用户本身销毁(例如,当它清除应用程序的缓存时)或在卸载应用程序时将其销毁。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.