0

Here is my code. It just involves 2 buttons and a number counter that goes up and down as the buttons are pressed (both in integer value and text size). This activity is part of a larger application that follows the process: 1. splash screen 2. list menu 3. activity selected in menu (there are 3 options and the counter is one of them)

When i press the back button, it goes back to the list menu and when i click on the integer counter option, i want it to be restored exactly how it was Any ideas on how i can do this? Thanks!

public class StartingPoint extends Activity {
/** Called when the activity is first created. */

int counter;
Button add;
Button sub;
TextView display;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    counter=0;

    add= (Button) findViewById(R.id.bAdd);
    sub= (Button) findViewById(R.id.bSub);
    display= (TextView) findViewById(R.id.tvDisplay);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter++;
            display.setText(""+counter);
            display.setTextSize(counter);
        }
    });
    sub.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            counter--;
            display.setText(""+counter);
            display.setTextSize(counter);
        }
    });

}

}

user947659
  • 2,485
  • 4
  • 21
  • 24

1 Answers1

2

You have two good options:

Either use SharedPreferences to store your variable onStop() and load it onStart() - How to use SharedPreferences in Android to store, fetch and edit values;

or, if it is just an integer, you can use the startActivityForResult method to instantiate your integer counter activity and capture the result from the activity, as explained in Android: Capturing the return of an activity.

Hope it helps!

Community
  • 1
  • 1
jcxavier
  • 2,232
  • 1
  • 15
  • 24
  • You can also use static data, the file system, or a database. – Paul Nikonowicz Nov 29 '11 at 21:19
  • True. Those seemed to me the most straightforward at that moment to me, but the ones you suggested are perfectly viable as well. – jcxavier Nov 29 '11 at 21:31
  • if i used sharedpreferences on onStop(), then i exited out of the application using taskkiller or something...will the data stored using sharedpreferences be deleted? – user947659 Nov 29 '11 at 22:36
  • No. Citing from http://developer.android.com/guide/topics/data/data-storage.html#pref , "The SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types. You can use SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed)." – jcxavier Nov 29 '11 at 22:48