How to declare global variables in Android?

前端 未结 17 2959
死守一世寂寞
死守一世寂寞 2020-11-21 04:12

I am creating an application which requires login. I created the main and the login activity.

In the main activity onCreate method I added the following

17条回答
  •  梦谈多话
    2020-11-21 05:06

    Like there was discussed above OS could kill the APPLICATION without any notification (there is no onDestroy event) so there is no way to save these global variables.

    SharedPreferences could be a solution EXCEPT you have COMPLEX STRUCTURED variables (in my case I had integer array to store the IDs that the user has already handled). The problem with the SharedPreferences is that it is hard to store and retrieve these structures each time the values needed.

    In my case I had a background SERVICE so I could move this variables to there and because the service has onDestroy event, I could save those values easily.

提交回复
热议问题