简体   繁体   中英

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. 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.

I would use the SharedPreferences library. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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