简体   繁体   中英

How to hide an activity after login in android?

I have developed an App where the user has to login into the app using the login Activity. After successful login, the app is directed to Activity B. The app doesn't provide any signout options.So next time when the user uses the app I want to hide the login Activity's layout. Is it possible to do so?If yes, how is it possible?

Keep a variable in sharedpreferences and check it when your login activity starts

in LoginActivity, when the user press login button and if success,

 SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
 editor.putInt("loginStatus", 1);// removed "" 
 editor.commit();

Then in onCreate check the variable,

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
int restoredText = prefs.getInt("loginStatus", 0);
if (restoredText == 1) {
  // start activity
  finish(); // destroy login so user can't come back with back button
}

In LoginActivity do as Suggested by nr4bt but instead of int Put & get Boolean Value in SharedPreference.

write this code

SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
editor.putValue("loginStatus", true);
editor.commit();

& onCreate() of LoginActivity Write this code

SharedPreferences prefs = getPreferences(MODE_PRIVATE); 
String restoredText = prefs.getBoolean("loginStatus", false);
if (restoredText == true) {
 // start activity
  finish(); // destroy login so user can't come back with back button
 }

Hope this helps you.

It is quite simple to achive with SharedPreferences . You just save that user has loged in and later on start check if he already done it or not and act properly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM