I'm having an issue with FLAG_ACTIVITY_CLEAR_TOP. When the user launch the app a screen appear for them to login or register. Once the user login into the app I want all the previous Activities to close. When I press the back button it logs the user out and takes them back to the LAUNCHER activity.
Below is my login Activity:
public class login extends AsyncTask<String, String, String>{
//Declaring global variables to be used throughout asyn class
String email;
String password;
UserFunctions userFunction;
JSONObject json;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(LoginActivity.this);
pDialog.setMessage("Logging into Thryfting...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
publishProgress();
email = inputEmail.getText().toString();
password = inputPassword.getText().toString();
userFunction = new UserFunctions();
json = userFunction.loginUser(email, password);
// check for login response
try {
//if (json.getString(KEY_SUCCESS) != null) {
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// user successfully logged in
// Store user details in SQLite Database
DatabaseHandler db = new DatabaseHandler(getApplicationContext());
JSONObject json_user = json.getJSONObject("user");
// Clear all previous data in database
userFunction.logoutUser(getApplicationContext());
db.addUser(json_user.getString(KEY_NAME), json_user.getString(KEY_EMAIL), json.getString(KEY_UID), json_user.getString(KEY_CREATED_AT));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
pDialog.dismiss();
email = inputEmail.getText().toString();
password = inputPassword.getText().toString();
userFunction = new UserFunctions();
json = userFunction.loginUser(email, password);
try {
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1){
// Launch Dashboard Screen
Intent dashboard = new Intent(getApplicationContext(), Timeline.class);
// Close all views before launching Dashboard
dashboard.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(dashboard);
// Close Login Screen
finish();
}
else{
// Error in registration
Toast.makeText(getApplicationContext(), "Wrong Email and password combination", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
If you are calling finish()
to your login acivity then FLAG_ACTIVITY_CLEAR_TOP
will not work.
Remove the finsh()
from your login activity from here
startActivity(dashboard);
// Close Login Screen
finish(); //remove this
in your onPostExceute method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With