简体   繁体   English

关闭应用程序流程后如何保存搜索栏位置?

[英]How can i save my seekbar position after closing the app process?

I'm trying to save the position of a SeekBar after closing the process, not only when I put the application in background, but even when i kill the process. 我试图在关闭进程后保存SeekBar的位置,不仅在将应用程序置于后台时,甚至在我终止进程时也是如此。 How can I do it? 我该怎么做?

One way is to use shared preferences: 一种方法是使用共享首选项:

SharedPreference sp = PreferenceManager.getDefaultSharedPreferences(this);
Editor e = sp.edit();
int seekbar = seekbar.getProgress();
e.putInt("BarPosition", seekbar).commit();

then whenever you load your view get it from shared pref: 然后,每当您加载视图时,都会从共享首选项中获取视图:

int sp = mSharedPrefs.getInt("BarPosition", 0);
seekbar.setProgress(sp);

You can call these on onPause() method when app is in background/killed. 当应用程序处于后台/已onPause()时,可以在onPause()方法上调用它们。

I would use the SharedPreferences library. 我将使用SharedPreferences库。 For example: 例如:

// 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);

This values will be persisted in the app's cache, until they will be destroyed by the user itself (for example when it clears the app's cache) or when the app is uninstalled. 此值将保留在应用程序的缓存中,直到它们被用户本身销毁(例如,当它清除应用程序的缓存时)或在卸载应用程序时将其销毁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM