0

Possible Duplicate:
Making data persistent in android

Edited:

I am building an android app which works on the principle of a simple counter.

public class TasbeehActivity extends Activity {
/** Called when the activity is first created. */
int count;
ImageButton imButton;
TextView display;
static String ref = "Myfile";
static String key = "key";
SharedPreferences myPrefs;
boolean check = false;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    imButton = (ImageButton) findViewById(R.id.bCount);
    display = (TextView) findViewById(R.id.tvDisplay);
     myPrefs = getSharedPreferences(ref, 0);

     if(check){
         String dataReturned = myPrefs.getString(key, "0");
            count = Integer.parseInt(dataReturned);
            display.setText(""+count);
     }

    imButton.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Random rand = new Random();
            display.setTextColor(Color.rgb(rand.nextInt(255),
                    rand.nextInt(255), rand.nextInt(255)));
            count++;
            display.setText("" + count);
        }
    });

}

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

     String data = display.getText().toString(); SharedPreferences.Editor
     editor = myPrefs.edit(); editor.putString(key, data);
     editor.commit();
     check = true;

}}

I want to save the value of count so that when my app restarts after closing it should have the value of count before closing app. Also when I change orientation of the emulator i.e. from portrait to landscape or vice versa count gets set to 0. Is there any solution for both of these problems?

4
  • i think you should use [SharePrefrences ](developer.android.com/reference/android/content/…) Commented Jun 28, 2012 at 12:05
  • use intent on android set it and when open the app just read it back Commented Jun 28, 2012 at 12:05
  • Save count value in SharedPreferences. you able to access it next time when app open again. Thanks. Commented Jun 28, 2012 at 12:06
  • I have saved the count value to SharedPreferences in onDestroy as at developer.android.com it is mentioned that when an app changes orientation it goes through onDestroy to onCreate. But When try to access this value in onCreate it gives an error and application crashes because it tries to get value before set -ing. is there any other way out? Commented Jun 29, 2012 at 13:20

3 Answers 3

0

You should use SharedPreferences to save your variable, and Override OnConfigurationChange because it's probably calling your oncreate method (don't forget to add android:configChanges="orientation|keyboardHidden|screenSize" in your manifest) I Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

0

1. use sharedpreferences for saving or retrieving value of count when app is restarts after closing

2. see handle orientation change in android count gets set to 0 when I change orientation of the emulator i.e. from portrait to landscape or vice versa

Comments

0

Try these....

  1. android:configChanges="orientation" , This will prevent your Activity to again restart when orientation changes.

  2. Use Bundle, but thats only for small amount of data. Serialization and Deserialization will be needed.

  3. You can also use onRetainNonConfigurationInstance(), and getLastNonConfigurationInstance(), for storing and retrieving data.

See this link for further details:

http://developer.android.com/guide/topics/resources/runtime-changes.html

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.