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
The suggested by Soonil way of keeping a state for the application is good, however it has one weak point - there are cases when OS kills the entire application process. Here is the documentation on this - Processes and lifecycles.
Consider a case - your app goes into the background because somebody is calling you (Phone app is in the foreground now). In this case && under some other conditions (check the above link for what they could be) the OS may kill your application process, including the Application
subclass instance. As a result the state is lost. When you later return to the application, then the OS will restore its activity stack and Application
subclass instance, however the myState
field will be null
.
AFAIK, the only way to guarantee state safety is to use any sort of persisting the state, e.g. using a private for the application file or SharedPrefernces
(it eventually uses a private for the application file in the internal filesystem).