-3

How to store value in Sharedpreferences when user quits from my application?

I have tried this code but it's not working for me:

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    //enter code here
    super.onDestroy();
    editor.putString("Check_activity", "true");
    editor.commit();
}  
Harshad
  • 99
  • 2
  • 9

2 Answers2

3

Note that onStop() and onDestroy() are usually killable. After that method returns the process hosting the activity may killed by the system at any time without another line of its code being executed. Because of this, you should use the onPause() method to write any persistent data (such as user edits) to storage.

In addition, the method onSaveInstanceState(Bundle) is called before placing the activity in such a background state, allowing you to save away any dynamic instance state in your activity into the given Bundle, to be later received in onCreate(Bundle) if the activity needs to be re-created.

For example, consider the following application code snippet:

public class DemoActivity extends Activity {
     ...

 static final int DATA_ONE = 0;
 static final int DATA_TWO = 1;

 private SharedPreferences mPrefs;
 private int mData;

 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     SharedPreferences mPrefs = getSharedPreferences();
     mData = mPrefs.getInt("data_no",DATA_ONE);
 }

 protected void onPause() {
     super.onPause();

     SharedPreferences.Editor ed = mPrefs.edit();
     ed.putInt("data_no", mData);
     ed.commit();
 }
 }
TryinHard
  • 4,078
  • 3
  • 28
  • 54
  • In a pre-Honeycomb device onPause is guaranteed to be called back last. On post-Honeycomb devices onStop is guaranteed to be the last so in that case any persistent data can be stored in onStop instead onPause – TheoK Jun 26 '15 at 06:28
  • @TheoK `Note that this method may never be called, in low memory situations where the system does not have enough memory to keep your activity's process running after its onPause() method is called.` http://developer.android.com/reference/android/app/Activity.html#onStop() – Phantômaxx Jun 26 '15 at 06:36
  • @DerGolem `Be aware that these semantics will change slightly between applications targeting platforms starting with HONEYCOMB vs. those targeting prior platforms. Starting with Honeycomb, an application is not in the killable state until its onStop() has returned. This impacts when onSaveInstanceState(Bundle) may be called (it may be safely called after onPause() and allows and application to safely wait until onStop() to save persistent state.` http://developer.android.com/reference/android/app/Activity.html (The same is also mentioned in the Udacity tutorials created by Google) – TheoK Jun 26 '15 at 06:49
1

onDestroy() is not necessarily called if the user quits the application using the home button.

Hav a look at Android Activity for life cycle of an android app.
Choose another function, e.g. onPause() to save important information.

Sven Jung
  • 331
  • 1
  • 2
  • 9