Gabriele Puia
Gabriele Puia

Reputation: 333

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?

Upvotes: 1

Views: 332

Answers (2)

Domenico
Domenico

Reputation: 1491

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.

Upvotes: 0

Zaki
Zaki

Reputation: 5600

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.

Upvotes: 2

Related Questions