Aby to osiągnąć SharedPreferences
można zrobić coś takiego:
Wstaw następujący w dowolnej klasy widać bardziej pasuje. Załóżmy, że wstawiłeś to w klasie Example
.
//Give your SharedPreferences file a name and save it to a static variable
public static final String PREFS_NAME = "MyPrefsFile";
Teraz w metodzie, która ocenia, czy użytkownik loguje się powodzeniem, wykonaj następujące czynności. Zwróć uwagę na klasę Example
, musisz to zmienić, aby pasowała do twojego kodu.
//User has successfully logged in, save this information
// We need an Editor object to make preference changes.
SharedPreferences settings = getSharedPreferences(Example.PREFS_NAME, 0); // 0 - for private mode
SharedPreferences.Editor editor = settings.edit();
//Set "hasLoggedIn" to true
editor.putBoolean("hasLoggedIn", true);
// Commit the edits!
editor.commit();
Wreszcie gdy aplikacja rozpoczyna można teraz ocenić, czy użytkownik jest już zalogowany lub nie. Wciąż zauważysz klasę Example
, którą musisz zmienić.
SharedPreferences settings = getSharedPreferences(Example.PREFS_NAME, 0);
//Get "hasLoggedIn" value. If the value doesn't exist yet false is returned
boolean hasLoggedIn = settings.getBoolean("hasLoggedIn", false);
if(hasLoggedIn)
{
//Go directly to main activity.
}
Nadzieja to pomaga
EDIT: Aby uniemożliwić użytkownikowi za pomocą przycisku Wstecz, aby wrócić do aktywności Logowanie trzeba finish()
aktywność po uruchomieniu nowego.
następujący kod pochodzi z Forwarding.java | Android developers
// Here we start the next activity, and then call finish()
// so that our own will stop running and be removed from the
// history stack
Intent intent = new Intent();
intent.setClass(Forwarding.this, ForwardTarget.class);
startActivity(intent);
Example.this.finish();
tak, co trzeba zrobić w kodzie jest wywołanie funkcji finish()
na aktywność logowania, po wywołaniu startActivity()
.
Zobacz także: Removing an activity from the history stack
Link, który próbowałeś nam udostępnić, był nieprawidłowy. Edytuj swoje pytanie, aby dodać poprawny link. –