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?

Community
  • 1
  • 1
mhaseebn
  • 541
  • 3
  • 8
  • 19
  • use intent on android set it and when open the app just read it back – Vitaly Menchikovsky Jun 28 '12 at 12:05
  • Save count value in SharedPreferences. you able to access it next time when app open again. Thanks. – Md Abdul Gafur Jun 28 '12 at 12:06
  • i think you should use [SharePrefrences ](http://developer.android.com/reference/android/content/SharedPreferences.html) – Samir Mangroliya Jun 28 '12 at 12:05
  • 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? – mhaseebn Jun 29 '12 at 13:20

3 Answers3

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

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
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.

AMerle
  • 4,354
  • 1
  • 28
  • 43
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

Kumar Vivek Mitra
  • 33,294
  • 6
  • 48
  • 75