narimetisaigopi

MainActivity.java sp

Feb 15th, 2020
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. package com.example.saigopinarimeti.helloworld;
  2.  
  3. import android.content.Intent;
  4. import android.content.SharedPreferences;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import android.widget.Toast;
  11.  
  12. public class MainActivity extends AppCompatActivity {
  13.  
  14. EditText editText;
  15. Button login;
  16.  
  17. SharedPreferences sharedPreferences;
  18. SharedPreferences.Editor editor;
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23.  
  24. editText = findViewById(R.id.username);
  25. login = findViewById(R.id.login);
  26.  
  27. sharedPreferences = getSharedPreferences("user",MODE_PRIVATE);
  28. editor = sharedPreferences.edit();
  29.  
  30.  
  31. if (sharedPreferences.getBoolean("isLoggedIn",false)){
  32. startActivity(new Intent(this,HomeActivity.class));
  33. Toast.makeText(this, "already logged in", Toast.LENGTH_SHORT).show();
  34. }else{
  35. Toast.makeText(this, "please loggin", Toast.LENGTH_SHORT).show();
  36. }
  37.  
  38.  
  39. login.setOnClickListener(new View.OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. editor.putString("username",editText.getText().toString()).apply();
  43. editor.putBoolean("isLoggedIn",true).apply();
  44. }
  45. });
  46.  
  47.  
  48.  
  49.  
  50. }
  51.  
  52. }
Add Comment
Please, Sign In to add comment