Issue
I've some information in my application , it's my company information . I want to make a login every time app closes and runs .
this is what i've done so far . I make a custom dialog with user and password field . The problem is this , every time I rotate the screen or I come to MainActivity , I need to re login and it's bothering alot .
I want this dialog comes up every time I close the app and run it again , just that . I've tried onStop and onDestory but when I rotate the screen or change the activity and leave mainactivity , it show me this dialog .
How can I show the dialog only the first time app runs and if I close the app and run it again ,it asks me to login again ?
thanks
Solution
On orientation change, all life cycle functions of the Activity are called, because it is reconstructed from zero. Your application must take this into account. The current activity goes onPause() onStop() onDestroy(), then the new Activity goes onCreate() onStart() onResume(), in that order.
Therefore you must store the fact that you have logged in, in such a way that the application does not forget. Peristent data storage can be done with SharedPreferences for example, if you don't want to use anything more complicated. Check out Android getDefaultSharedPreferences for this.
What you could probably do (but it seems like a haxory solution rather than a good one) is that in the onSaveInstanceState(Bundle bundle) function you save out the current value of the boolean that indicates being logged in into the Bundle, and then in onDestroy() you set it to false in the SharedPreferences. In the onCreate(Bundle bundle) function, if the Bundle isn't null, then update the SharedPreferences' boolean according to the Bundle's content. If the user is "logged in" according to the boolean, don't display the dialog.
Answered By - EpicPandaForce
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.