Advertisement
ZazoTazo

Shared Preferences Login Page (Failed)

Jul 27th, 2019
445
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.94 KB | None | 0 0
  1. package com.rcc.www.loginpage;
  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.EditText;
  9.  
  10. public class MainActivity extends AppCompatActivity
  11. {
  12.     EditText name, pass;
  13.     SharedPreferences savedData;
  14.  
  15.     @Override
  16.     protected void onCreate(Bundle savedInstanceState)
  17.     {
  18.         super.onCreate(savedInstanceState);
  19.         setContentView(R.layout.activity_main);
  20.  
  21.         name = findViewById(R.id.username);
  22.         pass = findViewById(R.id.password);
  23.  
  24.         savedData = getSharedPreferences("file", 0);
  25.         String username = savedData.getString("name", "No user found.");
  26.         if(!username.equals("No user found.")){
  27.             Intent intent = new Intent(MainActivity.this, Main2Activity.class);
  28.             startActivity(intent);
  29.         }
  30.  
  31.     }
  32.  
  33.     public void login(View view){
  34.        /* savedData = getSharedPreferences("file", 0);
  35.         SharedPreferences.Editor pin = savedData.edit();
  36.         pin.putString("name", name.getText().toString());
  37.         pin.putInt("password", Integer.parseInt(pass.getText().toString()));
  38.         pin.apply();
  39.         Intent intent = new Intent(MainActivity.this, Main2Activity.class);
  40.         //Bundle data = new Bundle();
  41.         //data.putString("Name", name.getText().toString());
  42.         //data.putInt("Password", Integer.parseInt(pass.getText().toString()));
  43.         //intent.putExtras(data);
  44.         startActivity(intent);*/
  45.        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
  46.        String username = name.getText().toString();
  47.        int password = Integer.parseInt(pass.getText().toString());
  48.        SharedPreferences.Editor pen = savedData.edit();
  49.  
  50.        pen.putString("Name", username);
  51.        pen.putInt("Password", password);
  52.        pen.apply();
  53.        startActivity(intent);
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement