Public var vs. passing same value multiple times between activities

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?

Best Answer

Answers

  • mirkarammirkaram Member ✭✭

    @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...

  • thebigbothebigbo Member ✭✭

    Thanks a lot for your answers! I'll be using SharedPreferences.

Sign In or Register to comment.