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