OnRestoreInstanceState got a problem in Xamarin

384 views Asked by At

Am working on an alarm app, and my layout happens to contain a TimePicker View used to set the time the user wishes to be reminded of something. I seem to have a problem with the methods applied in saving values when the Activity is being sent to the background. The code in the compiler is clean with no errors and yet when I debug it on smartphone, I send the Application to the background and then when resume it the TimePicker current hour is not modified by these two methods...Your time and contributions are well appreciated

 class SecondActivity : AppCompatActivity
    {
      TimePicker timePicker2;
       int x=2;
       protected override void OnCreate(Bundle savedInstanceState)
        {
         base.OnCreate(savedInstanceState);
         //Timepicker definition
         timepicker1=this.FindViewById<TimePicker>(Resource.Id.timepicker1);
          //Setting the default selected time of the Timepicker via OnCreate
        timepicker1.CurrentHour=Java.Lang.Integer.ValueOf(x);

        }
     //This method supposedly overrides another method called OnSavedInstanceState and allows programmer to save values to it
      protected override void OnSaveInstanceState(Bundle savedInstanceState)
        {
            int y;
            y = 2;
         //Saving my value to Bundle savedInstanceState
            savedInstanceState.PutInt("value",y);
           //This super is being called but i dont know what it does
            base.OnSaveInstanceState(savedInstanceState);
        }
     //This method is supposed to remember all the values user saved before the Activity went into OnPause()
//Changed this from public to protected because i figured onsavedInstanceState has its parameters protected so unless its an inheritance, access to its members is forbidden
        protected override void OnRestoreInstanceState(Bundle savedInstanceState)
        {
            base.OnRestoreInstanceState(savedInstanceState);
            //Restore the time value from the SavedInstance state
            int c = savedInstanceState.GetInt("value");
           //Assign the value to The Timepicker
            timePicker2.CurrentHour=(Java.Lang.Integer.ValueOf(c));
        }
}````
What am i doing wrong? An additional alert dialog code to detect when activity goes into Onpause and OnRestore will greatly be appreciated
1

There are 1 answers

0
Junior Jiang On BEST ANSWER

I seem to have a problem with the methods applied in saving values when the Activity is being sent to the background.

You could have a check with this Activity.OnRestoreInstanceState Method to know:

This method is called after OnStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState.

enter image description here

This method will not be called when you back from background, it only be called after being re-initialized. Such as Recreate() method will invoke it.

Therefore, here when entering to background, the OnSaveInstanceState method works. However, OnRestoreInstanceState will not be called.

Now if you want to sotre some data and restore view between background and foreground, you could use SharedPreferences in OnResume() and OnPause() of Activity.

For example, code as follows:

protected override void OnResume()
{
    base.OnResume();
    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
    int value = prefs.GetInt("value", 0);
    timePicker1.CurrentHour = (Java.Lang.Integer.ValueOf(value));
}


protected override void OnPause()
{
    base.OnPause();
    ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
    ISharedPreferencesEditor editor = prefs.Edit();
    int y;
    y = 2;
    editor.PutInt("value", y);
    editor.Apply();
}

Then the effect will show:

enter image description here