简体   繁体   中英

Using static variables to store user info on Android

I'm making an android app which has to frequently call some .net web services using the kSoap2 library. The user has to log in each time the app is launched (a decision that is out of my hands). I have one class which calls the web services. Each web service requires a username, a password and one other data string to access it. When the user successfully logs in I store the correct username, password and other piece of data in 3 private static variables inside the web services class, to use when I need to call other web services.

Could this approach end up being problematic down the road?

Have a look at SharedPreferences , sounds like exactly what you need. Static variables are generally bad programming form.

Could this approach end up being problematic down the road?

I don't see why this would be a problem.

As for Ralgha's answer, Shared preferences persist your login information to an XML file. If the user must login to the application every time anyway, it defeats the purpose of the shared preference. You are better off keeping this data in memory.

The VM cannot GC any static variable you hold a reference to. So the data will not be GCed. Other applications cannot read in to your memory either. Sounds all right to me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM