gpt4 book ai didi

java - FLAG_ACTIVITY_CLEAR_TOP 无效

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:17:45 25 4
gpt4 key购买 nike

我遇到了 FLAG_ACTIVITY_CLEAR_TOP 问题。当用户启动应用程序时,会出现一个屏幕供他们登录或注册。用户登录到应用程序后,我希望关闭所有以前的 Activity 。当我按下后退按钮时,它会将用户注销并将他们带回 LAUNCHER Activity 。

下面是我的登录 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();
}

最佳答案

如果您调用 finish() 到您的登录 Activity ,则 FLAG_ACTIVITY_CLEAR_TOP 将不起作用。

从此处的登录 Activity 中删除 finsh()

startActivity(dashboard);

// Close Login Screen
finish(); //remove this

在您的 onPostExceute 方法中。

关于java - FLAG_ACTIVITY_CLEAR_TOP 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12039874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com