Best practices for handle session in an Android App

hi, i’m looking for an advice on how to handle a session object in an Android App, this object holds the user name and session token to perform network request from different points in the app, i have read about singleton pattern but i have a problem when the token expires, reload the singleton seems a bad practice, in some iOS tutorias here in this site there’s a multicast closure delegate pattern to handle the changes, there’s a good practice in kotlin/ android to handle a session object without using a singleton and dealing with the global state problem of singletones
Thanks in Advance

@macsimus Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi @spaceghost87 thanks for the question!

The answer depends somewhat on how your API handles sessions and authentication, e.g. is it using OAuth 2. A method I’ve had success with in the past is using an OkHttp Interceptor or Authenticator implementation to handle the session and tokens. We’ll try and do a screencast on this topic soon, but in the meantime, if you look up “Retrofit authentication” you should see some good results. Thanks again!

thanks for the reply, my problem is that i have a model ‘User’ class with a token property, each time i have to perform a request i need to access the user class and fetch the token in order to make a request, so is a nightmare for deadlocks or priority inversion when i try to lock the token, also if some request get an 401 unauthorized, i need to ask the user to re enter the password
handle all this seems like a common but unsolved problem

This topic was automatically closed after 166 days. New replies are no longer allowed.