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
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();
afterstartActivity()
To prevent this accidental login you should check sharedPreferences value is set before. Back button disabling is not a good option.
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
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.
You need to
finish()
all previousActivity
when logging out. Try the code below You can emit flagIntent.FLAG_ACTIVITY_NEW_TASK
.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.You can override onBackPressed on login page, then when users click on back, you can handle it your way:
Navigate into login activity if click logout button and listen for back pressed on login activity
Intent ... finish();
onBackPressed();
Changed isLogin Flag after success login , Update your Login Activity Code,