Search code examples
androidxamarinstates

How to save all states in portrait mode Android


I want to know how if Android OS provide to store states without using "OnSavedInstance" ? For .e.g I have main activity with some controls(textboxes) and for some reason,user pressing to home button and for this case,I want to store all data,that was introduced to this controls. Is this possible to not destroy/recreate Activity,but just store into Memory of phone(RAM) ?


Solution

  • i want to know how if Android OS provide to store states without using "OnSavedInstance" Yes,Android OS provide this.
    It's pretty simple,what we need to do is :

    [Activity(Label = "YourActivityName",Icon = "@drawable/icon",ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize )]
    

    That is what you need exactly.
    Magic is here:

    ScreenOrientation = Android.Content.PM.ScreenOrientation.Portrait, ConfigurationChanges = Android.Content.PM.ConfigChanges.Orientation | Android.Content.PM.ConfigChanges.ScreenSize )
    

    Now when you will rotate your phone or press the home button,activity will be NOT destroyed(that means,OnCreate() Method will not be called again and again to recreate activity). Enjoy!