Here is my MainActivity.java for using just login into system without signup by using Shared Preferences. When I clicked Sign in button it will save input data into Preference. Now I need to use session for checking user logged in or not logged out. But I don't able to do this. Please help me. Advance Thanks 👍
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import api.syntech.com.crmapp.MenuItem.Dashboard;
//login class
public class MainActivity extends AppCompatActivity {
private EditText Email, Password;
private Button btn_signin;
private TextView errordia;
private SharedPreferences pref;
private static final String FILE_NAME = "UserInfo";
private String PARAM_ONE = "Email";
private String PARAM_TWO = "Password";
private String PARAM_THREE = "userLoggedInState";
private boolean isUserLoggedIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Email = (EditText)findViewById(R.id.email);
Password = (EditText)findViewById(R.id.password);
btn_signin = (Button)findViewById(R.id.btn_login);
errordia = (TextView)findViewById(R.id.errordia);
//SharedPref
btn_signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SaveData();
Login();
}
});
}
private void SaveData(){
String mEmail = Email.getText().toString();
String mPass = Password.getText().toString();
if (mEmail.equals("faruk") && mPass.equals("1234")){
SharedPreferences pref = getApplicationContext().getSharedPreferences(FILE_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(PARAM_ONE, mEmail);
editor.putString(PARAM_TWO, mPass);
editor.putBoolean(PARAM_THREE, true);
editor.commit();
Toast.makeText(getApplicationContext(), "Data is saved successfully", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getApplicationContext(), "Provide Valid Information",Toast.LENGTH_LONG).show();
}
}
private void LoadData(){
SharedPreferences pref = getApplicationContext().getSharedPreferences(FILE_NAME, MODE_PRIVATE);
String saved_email = pref.getString(PARAM_ONE, null);
String saved_password = pref.getString(PARAM_TWO, null);
errordia.setText("Email : " + saved_email + " Password: " + saved_password);
}
private void Login(){
SharedPreferences pref = getApplicationContext().getSharedPreferences(FILE_NAME, MODE_PRIVATE);
if (pref.contains(PARAM_ONE) && pref.contains(PARAM_TWO)){
Intent intent = new Intent(MainActivity.this, MenuPage.class);
startActivity(intent);
}
}
}
Try making the third parameter false when the user logs out or the app is stopped or destroyed:
private void LogOut(){
SharedPreferences pref = getApplicationContext().getSharedPreferences(FILE_NAME, MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean(PARAM_THREE, false);
editor.commit();
}
@Override
protected void onDestroy() {
super.onDestroy();
LogOut();
}
Now in your onCreate()
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences pref = getApplicationContext().getSharedPreferences(FILE_NAME, MODE_PRIVATE);
if(pref.getBoolean(PARAM_THREE, false))
{
Login();
}
else{
setContentView(R.layout.activity_main);
Email = (EditText)findViewById(R.id.email);
Password = (EditText)findViewById(R.id.password);
btn_signin = (Button)findViewById(R.id.btn_login);
errordia = (TextView)findViewById(R.id.errordia);
btn_signin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
SaveData();
Login();
}
});
}
}
Add thge logout button on your app.
When user click on logout clear the SharedPreferences login data.
SharedPreferences sharedPreferences = getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.clear().commit();
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.