Logout. back button pressed, again logged in

2019-07-29 12:19发布

From this page I am logging out. It is logging out properly but when back button is pressed it gets logged in again. i have given a proper intent function but yet it is not acting as per my commands. Please advice me a solution for this problem.

WELCOME PAGE CODE:-

public class Welcome extends AppCompatActivity {

    Button __btnlogout;
    Button btn1;
    Button btn2;
    DatagramSocketThread mDatagramSocketThread;

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

        __btnlogout = (Button)findViewById(R.id.btnLogout);


        __btnlogout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
                SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean("isLogin", false);
                editor.commit();
                Intent intent = new Intent(Welcome.this,
                        login.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);

            }
        });




        btn1 = (Button)findViewById(R.id.btn11);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("MainActivity", "22");

                mDatagramSocketThread = new DatagramSocketThread();
                mDatagramSocketThread.start();
                Intent intent = VpnService.prepare(getApplicationContext());
                if (intent != null) {
                    startActivityForResult(intent, 0);
                } else {
                    onActivityResult(0, RESULT_OK, null);
                }
            }
        });
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            Log.e("MainActivity", "23");

            Intent intent = new Intent(Welcome.this, MyClass.class);
            Log.e("MainActivity", "24");

            startService(intent);
        }

        btn2 = (Button)findViewById(R.id.btn22);
        btn2.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Log.e("MainActivity", "25");

                Intent intent;
                intent = new Intent(Welcome.this, MyClass.class);
                Log.e("MainActivity", "26");

                stopService(intent);
            }
        });
    }
}

And i want to be in Login page when logged out and even after pressing back button.

LOGIN PAGE CODE:-

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




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





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

        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);


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






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

        __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","15");

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

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

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

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



            }
        });




    }
}

THANK YOU

标签: android login
6条回答
乱世女痞
2楼-- · 2019-07-29 12:33

I think you shouldn't clear the shared preferences properly or should check shared preferences value before rendering welcome activity. Then you should add finish(); after startActivity()

SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = pref.edit();
editor.putBoolean("isLogin", false);
editor.commit();
Intent intent = new Intent(Welcome.this,login.class);         
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();

To prevent this accidental login you should check sharedPreferences value is set before. Back button disabling is not a good option.

查看更多
beautiful°
3楼-- · 2019-07-29 12:35

You have to finish Welcome Activity when you are logging, so user will not be able to come back this activity again except when the log in again. You have to add finish(); on logout

Intent intent = new Intent(Welcome.this,login.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();

and in you login page you can also handle what you want to do when you press back button, either exit from app or do something else, by override onBackPressed(); method.

@Override
public void onBackPressed()
{
  super.onBackPressed();
}
查看更多
唯我独甜
4楼-- · 2019-07-29 12:38

You need to finish() all previous Activity when logging out. Try the code below You can emit flag Intent.FLAG_ACTIVITY_NEW_TASK.

Intent intent = new Intent(login.this, Welcome.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
finish();

About your question you said i want to be in Login page when logged out and even after pressing back button.
Thats not the proper behavior for any app . The app should close when you back press from last Activity on Stack . So do not disable onBackPressed() in Login Activity.

查看更多
Viruses.
5楼-- · 2019-07-29 12:42

You can override onBackPressed on login page, then when users click on back, you can handle it your way:

@Override
public void onBackPressed()
{
     //super.onBackPressed();  // disable this
}
查看更多
可以哭但决不认输i
6楼-- · 2019-07-29 12:44

Navigate into login activity if click logout button and listen for back pressed on login activity

Intent ... finish();

onBackPressed();

查看更多
别忘想泡老子
7楼-- · 2019-07-29 12:52

Changed isLogin Flag after success login , Update your Login Activity Code,

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




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





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

        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","14");

        __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","15");

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

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

                    if (cursor.getCount()>0) {
                        Log.d("login","18");
                        //cursor.moveToNext();
                        Log.d("login","19");
  SharedPreferences pref= PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
        SharedPreferences.Editor editor = pref.edit();
        editor.putBoolean("isLogin", true);
        editor.commit();

                        startActivity(new Intent(login.this, Welcome.class));
                        Toast.makeText(getApplicationContext(), "Login Successfully", Toast.LENGTH_LONG).show();
                    }

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



            }
        });




    }
}
查看更多
登录 后发表回答