Android: how to save a session on the back button?

My application contains two classes of activity. from the first action, the user can enter the system and from the second action, the user can view their project details. I support the session also with the exit button. In my application, if the user clicked the "Back" button from the second action, he goes to the "Applications" section (main menu), and after a while the user clicked on the icon of his application, he goes to the first action, not the second action, but the user has not logged out.
I want user should go to second activity not first activity if user already login . I tried, but it does not work, and yet I try.
How can I do the above material, please someone will offer me an answer.

-one
android
source share
2 answers

You must save the status of the current user in SharedPreferences .

In the onCreate method of your login activity, before calling setContentView (), you can check if the user is logged in. If this is true, open a new activity, wish startActivity () and call finish () for the current activity. If you are not just continuing the normal call to setContentView () .

Thus, logIn activity will not be displayed if it is indicated inside SharedPreferences to log in.

0
source share

You do not need to display the first activity. But in fact there is always a main activity. This should decide whether to start the login action directly or directly display user content. You can use the finish () function from the main action as soon as you start logging into the system or the activity of the content, so when the user drops back (), he will no longer see the main activity.

 MainActivity extends Activity { public void onCreate() { if isLogged() startActivity(new Intent(ContentActivity ...)); else startActivity(new Intent(LoginActivity...)); finish() } LoginActivity extends Activity { ... public void onLoginSuccess(String username) { // called from dialog OK button and login process success startActivity(new Intent(ContentActivity ...)); finish(); } } 
0
source share

All Articles