0

Here is the full code. The app is working perfectly but the only thing where I am facing problem is that ---> 1. When the user registers for the first time, the app registers properly but when I close the app and open the app it still shows the registration window where as it show show the Welcome window. 2. I have to enter the details again and again when I login where as it should do only one time login.. it should not ask again and again.

These are the problems. Please anyone help me to solve this problem.

the MainActivity code

public class MainActivity extends AppCompatActivity {
    SQLiteOpenHelper openHelper;
    SQLiteDatabase db;
    Button _btnreg, _btnlogin;
    EditText _txtfname, _txtlname, _txtpass, _txtemail, _txtphone;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        openHelper = new DatabaseHelper(this);
        _txtfname = (EditText)findViewById(R.id.txtfname);
        _txtlname = (EditText)findViewById(R.id.txtlname);
        _txtpass = (EditText)findViewById(R.id.txtpass);
        _txtemail = (EditText)findViewById(R.id.txtemail);
        _txtphone = (EditText)findViewById(R.id.txtphone);
        _btnlogin=(Button)findViewById(R.id.btnlogin);
        _btnreg=(Button)findViewById(R.id.btnreg);
        Log.d("MainActivity","40");


        _btnreg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("MainActivity","50");

                db=openHelper.getWritableDatabase();
                String fname=_txtfname.getText().toString();
                String lname=_txtlname.getText().toString();
                String pass=_txtpass.getText().toString();
                String email=_txtemail.getText().toString();
                String phone=_txtphone.getText().toString();
                insertdata(fname, lname,pass,email,phone);
                Toast.makeText(getApplicationContext(), "register successfully",Toast.LENGTH_LONG).show();
                Log.d("MainActivity","60");

            }
        });


        _btnlogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("MainActivity","70");
                Intent intent = new Intent(MainActivity.this, login.class);
                Log.d("MainActivity","80");
                startActivity(intent);
                Log.d("MainActivity","+90");
            }
        });

    }

    public void insertdata(String fname, String lname, String pass, String email, String phone) {

        Log.d("MainActivity","100");
        ContentValues contentValues = new ContentValues();
        contentValues.put(DatabaseHelper.COL_2, fname);
        contentValues.put(DatabaseHelper.COL_3, lname);
        contentValues.put(DatabaseHelper.COL_4, pass);
        contentValues.put(DatabaseHelper.COL_5, email);
        contentValues.put(DatabaseHelper.COL_6, phone);
        long id = db.insert(DatabaseHelper.TABLE_NAME, null, contentValues);
        Log.d("MainActivity","200");
    }
}

The databaseHelper code

public class DatabaseHelper extends SQLiteOpenHelper {
    public static final String DATABASE_NAME="register.db";
    public static final String TABLE_NAME="registeration";
    public static final String COL_1="ID";
    public static final String COL_2="FirstName";
    public static final String COL_3="LastName";
    public static final String COL_4="Password";
    public static final String COL_5="Email";
    public static final String COL_6="Phone";


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.d("DatabaseHelper","10");
        db.execSQL("CREATE TABLE " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,FirstName TEXT,LastName TEXT,Password TEXT,Email TEXT,Phone TEXT)");
        Log.d("DatabaseHelper","20");
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d("DatabaseHelper","30");
        db.execSQL("DROP TABLE IF EXISTS " +TABLE_NAME);
        Log.d("DatabaseHelper","40");
        onCreate(db);
    }
}

here is the Login Activity code

public class login extends AppCompatActivity {
    SQLiteDatabase db;
    SQLiteOpenHelper openHelper;
    Button __btnLogin;
    EditText __txtEmail,__txtPass;
    Cursor cursor;

    public static final String PREFS_NAME = "MyPrefsFile";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        Log.d("login","11");

        openHelper = new DatabaseHelper(this);
        db=openHelper.getReadableDatabase();
        __btnLogin = (Button)findViewById(R.id.btnLogins);
        __txtEmail = (EditText)findViewById(R.id.txtEmails);
        __txtPass = (EditText)findViewById(R.id.txtPasss);






        Log.d("login","22");

        __btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String email = __txtEmail.getText().toString();
                String pass = __txtPass.getText().toString();




                if (pass == "" || email == "") {

                    Toast.makeText(getApplicationContext(),"No Entry", Toast.LENGTH_LONG).show();

                }



                Log.d("login","33");

                cursor = db.rawQuery("SELECT * FROM "+ DatabaseHelper.TABLE_NAME + " WHERE " + DatabaseHelper.COL_5 + " =? AND " + DatabaseHelper.COL_4 + " =? ", new String[]{email,pass});
                Log.d("login","44");

                if(cursor!=null) {
                    Log.d("login","55");

                    if (cursor.getCount()>0) {
                        Log.d("login","66");
                        //cursor.moveToNext();
                        Log.d("login","77");
                        startActivity(new Intent(login.this, Welcome.class));
                        Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_LONG).show();
                    }

                    else {
                        Log.d("login","88");
                        Toast.makeText(getApplicationContext(),"Error", Toast.LENGTH_LONG).show();
                        Log.d("login","99");
                    }
                }



            }
        });
    }
}

THANK YOU

  • 1
    Use Shared Preferences. – SripadRaj Feb 27 '18 at 09:03
  • after successful login you can store a isLogin flag in SharedPreferences and on app launch check that flag and proceed to further activity – Arshad Feb 27 '18 at 09:12
  • As @SripadRaj said, i would use Shared Preferences to store user and encrypted pass in case the first login was successful. And if you have data in the Shared Preferences try to login with that data. – Ivan Feb 27 '18 at 09:16
  • Possible duplicate of [How to keep android applications always be logged in state?](https://stackoverflow.com/questions/12744337/how-to-keep-android-applications-always-be-logged-in-state) – SripadRaj Feb 27 '18 at 09:21

3 Answers3

0

By using shared Preferences

 SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                    SharedPreferences.Editor editor = settings.edit();
                    editor.putString("username_preferences", email);
                    editor.putString("password_preferences",password);
                     editor.commit();
balu
  • 444
  • 1
  • 3
  • 10
  • When i logout from the app it takes me to the login screen, but when i press back button it again takes me to the logged in page whereas it has to be in the login page why because it was already logged out. –  Mar 06 '18 at 08:26
  • when you logout the app, you should clear the SharedPrefetences – balu Mar 06 '18 at 08:37
0

when user first time do login that time to store user data into SharedPreferences like password and username.

then after login activity in onCreate() method read SharedPreferences data and put the if condition like below

    if (!TextUtils.isEmpty(password) && !TextUtils.isEmpty(userName)){
        Intent intent=new Intent((this,WelcomeActivity.class));
        startActivity(intent);
    }
    else{

    }
  • I have tried this method but the code is not accepting the intent(not able to initialize it in oncreate method) in LOGINACTIVITY. Please could you help with it. Thank You very much for the response. –  Feb 28 '18 at 06:35
  • if you have splash screen then put in that class otherwise put in login activity. and i hope you define all the activity in android manifest file. –  Feb 28 '18 at 07:54
0

Put boolean in Pref. after login or register success,

  SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
    SharedPreferences.Editor editor = pref.edit();
    editor.putBoolean("isLogin", true);
    editor.commit();

Change Boolean flag after logout success

 SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLogin", false);
editor.commit();

Add this condition in MainActivity(LAUNCHER Activity) before setContentView

    SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        if (pref.getBoolean("isLogin", false))
        {
         //Open you Welcome Activity  
 finish();
        }
Bhavya Gandhi
  • 507
  • 3
  • 10
  • When i logout from the app it takes me to the login screen, but when i press back button it again takes me to the logged in page whereas it has to be in the login page why because it was already logged out. what can be done for this problem?? –  Mar 06 '18 at 08:28
  • Okay i will check – Bhavya Gandhi Mar 06 '18 at 09:29
  • I will give you answer .Check https://stackoverflow.com/a/49127902/6011225 – Bhavya Gandhi Mar 06 '18 at 10:02
  • Can you help me in open vpn service? If so can please help me. I have the code i have stuck like I am not understanding where exactly is the problem. Can you just go through the code and give me any suggestion or solution that would be very much helpful. –  Mar 08 '18 at 06:58
  • Here the full code and in the login service I am applying this vpn service. The link to my vpn service code:- https://stackoverflow.com/q/49124164/9243500 –  Mar 08 '18 at 07:00
  • Sure let me check – Bhavya Gandhi Mar 08 '18 at 07:03
  • Thank You So Much. –  Mar 08 '18 at 07:07
  • Sorry didn't tell you anything about the code. Here is a rough explanation about code and app. I am building the VPN APP it will be like a parental control. When I install this app in the phone, the phone should get INTERNET with the DNS that I provide. Thats it. –  Mar 08 '18 at 07:25