I defined a public variable Username which is used in 4 other activities (Activity A, Activity B, Activity C and Activity D). I know this is not the correct way to pass data between activities and I noticed that after a certain time the variable is becoming null.
My first question would be, why does Username lose its value after a certain period of time (e. g. I unlock the device after 6 hrs and get null reference exception where Username is passed as an argument to a method).
Second question is, how should I pass Username between activities. Should I PutExtra when starting each new activity or there's a better way to do it? My activities are execute in following order: Act A --> Act B --> Act C --> Act D. That means I pass Username 3 times between activities?
@thebigbo
When activity is in background, system may recycle some resources and variables because of low-memory situations, such as when Android is starved for resources . So It maybe happen the case that you faced.
It is highly recommended that you use : ISharedPreferences, which is equivalent to the class SharedPreferences .
With it ,we can store simple key-value pairs, such as : account / password and so on .
For example, to save a true/false bool or string using some Context you can do the following:
ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(mContext); ISharedPreferencesEditor editor = prefs.Edit(); editor.PutBoolean("my_bool_value", false); editor.PutString("Username","jack"); // editor.Commit(); // applies changes synchronously on older APIs editor.Apply(); // applies changes asynchronously on newer APIs
Access the saved values:
//Access saved values using: ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(mContext); bool mBool = prefs.GetBoolean("my_bool_value", < default value >); string Username = prefs.GetString("Username", < default value >);
If you use this way ,you will not need to pass Username many times between activities. You just need to access the saved values if you want to use the value of Username.
For more information: https://forums.xamarin.com/discussion/4758/android-shared-preference
Answers
@thebigbo You must readout the activity life cycle in android.
The below links will demonstrate all about activity life cycle.
h_t_t_p_s://docs.microsoft.com/en-us/xamarin/android/app-fundamentals/activity-lifecycle/#onpause
remove underscore from link protocol and visit...
@thebigbo
When activity is in background, system may recycle some resources and variables because of low-memory situations, such as when Android is starved for resources . So It maybe happen the case that you faced.
It is highly recommended that you use : ISharedPreferences, which is equivalent to the class SharedPreferences .
With it ,we can store simple key-value pairs, such as : account / password and so on .
For example, to save a true/false bool or string using some Context you can do the following:
Access the saved values:
If you use this way ,you will not need to pass Username many times between activities. You just need to access the saved values if you want to use the value of Username.
For more information: https://forums.xamarin.com/discussion/4758/android-shared-preference
Thanks a lot for your answers! I'll be using SharedPreferences.