tags:

views:

66

answers:

2

I have searched through this and a few other sites for the answer, but I have been unable to find it. I am trying to save a boolean and an int using onSaveInstanceState and onRestoreInstanceState, but I can't seem to get it to work. It doesn't crash or anything, but it either isn't saving it or it isn't restoring it, or I am stupid and have no idea what I am doing.


Here is what I have for my activity, do I need to have it in my onCreate somewhere or something?

public class CannonBlast extends Activity {
/** Called when the activity is first created. */
private panel panelStuffz;

@Override
public void onCreate(Bundle savedInstanceState) {
    final Window win = getWindow();
    super.onCreate(savedInstanceState);
    panelStuffz = new panel(this);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    win.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    setContentView(panelStuffz);
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState){
    savedInstanceState.putInt("HighLevel", panelStuffz.getLevel());
    savedInstanceState.putBoolean("soundstate", panelStuffz.getSound());
    super.onSaveInstanceState(savedInstanceState);
}

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    panelStuffz.setHighLevel(savedInstanceState.getInt("HighLevel"));
    panelStuffz.setSound(savedInstanceState.getBoolean("soundstate"));
}

@Override
public void onResume(){
    super.onResume();
}

@Override
public void onPause(){
    super.onPause();
    panelStuffz.setThread(null);

}

@Override
public void onStop(){

}

I tried putting stuff in the onStop, but it crashes, which is why its empty, in case that matters, thanks in advance

A: 

This method is only for saving state associated with a current instance of an Activity,are you switching to another activity and trying to get the values again when you return to this activity? i supposed that :: "I tried putting stuff in the onStop, but it crashes".

a recommendation, is NOT SAFE to use onSaveInstanceState() and onRestoreInstanceState(), according to http://developer.android.com/reference/android/app/Activity.html

consider using Sharedpreferences or SQLite database.

SharedPreferences Example (from webworld):

 /**
   * get if this is the first run
   *
   * @return returns true, if this is the first run
   */
      public boolean getFirstRun() {
      return mPrefs.getBoolean("firstRun", true);
   }

   /**
   * store the first run
   */

   public void setRunned() {
      SharedPreferences.Editor edit = mPrefs.edit();
      edit.putBoolean("firstRun", false);
      edit.commit();
   }

   SharedPreferences mPrefs;

   /**
   * setting up preferences storage
   */
   public void firstRunPreferences() {
      Context mContext = this.getApplicationContext();
      mPrefs = mContext.getSharedPreferences("myAppPrefs", 0); //0 = mode private. only this app can read these preferences
   }
Jorgesys
o0o, I thought onSave WAS Sharepreferences :/ thanks, will look into that
William
save that values in SharedPreferences and you will restore that values on any activity of your app =)
Jorgesys
A: 

Many applications may provide a way to capture user preferences on the settings of a specific application or an activity. For supporting this, Android provides a simple set of APIs.

Preferences are typically name value pairs. They can be stored as “Shared Preferences” across various activities in an application (note currently it cannot be shared across processes). Or it can be something that needs to be stored specific to an activity.

  1. Shared Preferences: The shared preferences can be used by all the components (activities, services etc) off the applications.

  2. Activity handled preferences: These preferences can only be used with in the activity and can not be used by other components of the application.

Shared Preferences:

The shared preferences are managed with the help of getSharedPreferences method of the Context class. The preferences are stored in a default file(1) or you can specify a file name(2) to be used to refer to the preferences.

(1) Here is how you get the instance when you specify the file name

public static final String PREF_FILE_NAME = "PrefFile";
   SharedPreferences preferences = getSharedPreferences(PREF_FILE_NAME, MODE_PRIVATE);

MODE_PRIVATE is the operating mode for the preferences. It is the default mode and means the created file will be accessed by only the calling application. Other two mode supported are MODE_WORLD_READABLE and MODE_WORLD_WRITEABLE. In MODE_WORLD_READABLE other application can read the created file but can not modify it. In case of MODE_WORLD_WRITEABLE other applications also have write permissions for the created file.

(2) The recommended way is to use by the default mode, without specifying the file name

SharedPreferences preferences = PreferencesManager.getDefaultSharedPreferences(context);

Finally, once you have the preferences instance, here is how you can retrieve the stored values from the preferences:

 int storedPreference = preferences.getInt("storedInt", 0);

To store values in the preference file SharedPreference.Editor object has to be used. Editor is the nested interface of the SharedPreference class.

SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

Editor also support methods like remove() and clear() to delete the preference value from the file.

Activity Preferences:

The shared preferences can be used by other application components. But if you do not need to share the preferences with other components and want to have activities private preferences. You can do that with the help of getPreferences() method of the activity. The getPreference method uses the getSharedPreferences() method with the name of the activity class for the preference file name.

Following is the code to get preferences

SharedPreferences preferences = getPreferences(MODE_PRIVATE);
int storedPreference = preferences.getInt("storedInt", 0);

The code to store values is also same as in case of shared preferences.

SharedPreferences preferences = getPreference(MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt("storedInt", storedPreference); // value to store
editor.commit();

You can also use other methods like storing the activity state in database. Note Android also contains a package called android.preference. The package defines classes to implement application preferences UI.

To see some more examples check Android's Data Storage post on developers site.

Pentium10