1

I'm creating a simple app where the user can change the seekbar value. make the default value as 0 and setMax as 5. Every time the app is closed and started back, seekbar value is set to 0. How can I use and set the previous seekbar value which was set before closing the app.

Here's my Activity class >

public class MainActivity extends Activity {

SeekBar s;
TextView v;
public int n;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    s=(SeekBar)findViewById(R.id.seekBar1);
    v=(TextView)findViewById(R.id.textView1);
    next=(Button)findViewById(R.id.button1);

    s.setProgress(n);
    v.setText(n+" ");

    s.setOnSeekBarChangeListener(new OnSeekBarChangeListener(){

        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            // TODO Auto-generated method stub
            n = progress;
            v.setText(n+" ");

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
            v.setText(n+" ");
        }

    });
  }
}

I Tried using onSaveInstanceState and onRestoreInstanceState but failed to restore back to the previous state. Please Help

Thanks in Advance.

Abhijit
  • 41
  • 2
  • 9
  • Shared Preferences are what you want for this: http://stackoverflow.com/questions/3624280 – dymmeh Aug 20 '13 at 17:27
  • I tried every possible thing in shared Preferences. But failed to get the saved data. Please help me on that. I just want to save the progress i.e "n" , and retrieve it once the app is restarted – Abhijit Aug 22 '13 at 13:41

3 Answers3

2

First of all, onSaveInstance and onRestoreInstance method are triggered only, when a destruction because of orientation change happens, hence you can not use this mechanism with that purpose, in order to store previous values after explicitly destroying the activity, you can use one of the persistance mechanisms that android provides. SharedPreferences or SQLiteDB saving them before destroying the activity, and restoring when the activity is started.

To get the latest value if any you have to add this code in onCreate method:

private SharedPreferences prefs;
private int progress;

protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);        
 prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
 progress = prefs.getInt("myProgress", 0);

}

//And to store it all the time before the activity is destroyed you have to add this code in onPause:

protected void onPause() {
  super.onPause();
  Editor editPrefs = prefs.edit();
  editPrefs.putInt("myProgress", newProgressValue);
  editPrefs.commit();

}

Hope this helps.

Regards!

Martin Cazares
  • 13,637
  • 10
  • 47
  • 54
  • I tried every possible thing in shared Preferences. But failed to get the saved data. Please help me on that. I just want to save the progress i.e "n" , and retrieve it once the app is restarted. – Abhijit Aug 22 '13 at 13:39
  • Thanks a ton! this works. But one more issue is How do i check if the user is opening the app for the first time? – Abhijit Aug 22 '13 at 16:24
  • You can make use of preferences pretty much the way i did for progress, all you have to know is that prefs.getInt("yourTag", defaultValue), will return the default value when there's no preference stored in it and thats the sign for you to know it's been open for first time, after that inmediately you write something into it, so now you know the app previosly stored something, hence the app has been started before... You might want to use just a boolean in stead of int, by the way – Martin Cazares Aug 22 '13 at 16:50
1

You might want to look at Storage Options in the Developers guide.

Specifically, Shared Preferences seems like a good place to start.

Daniel Arndt
  • 2,268
  • 2
  • 17
  • 22
0

you can try to use SQL database, the database will be availble even after the app is closed and reopened

ad heat
  • 70
  • 6