我遇到了FLAG_ACTIVITY_CLEAR_TOP的问题。当用户启动应用程序时,会出现一个屏幕供他们登录或注册。一旦用户登录到应用程序,我希望所有以前的活动都关闭。当我按下后退按钮时,它会将用户注销并将其带回LAUNCHER活动。
以下是我的登录活动:
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();
}
答案 0 :(得分:1)
如果您致电finish()
登录活动,FLAG_ACTIVITY_CLEAR_TOP
将无效。
从此处
中删除登录活动中的finsh()
startActivity(dashboard);
// Close Login Screen
finish(); //remove this
在你的onPostExecute方法中。
答案 1 :(得分:0)
而不是使用addflags清除活动尝试这个
dashboard.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
这肯定会解决你的问题。