我很难让用户在退出后使用后退按钮离开我的应用。
我的HomeActivity检查用户是否有sessionID。如果他们不这样做,它会将他们重定向到登录页面。成功登录后,将显示主页,显示sessionID和注销按钮。
单击该按钮将删除会话ID,并向用户显示该消息。此时按下后退按钮会将用户带到登录页面,我无法通过按下来退出 - 我可以登录并按回去退出应用程序,但我无法退出注销后返回按钮。
任何想法?
HomeActivity
public class HomeActivity extends Activity {
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
}
protected void onStart(){
super.onStart();
/*
* Check if a session ID exists - if it does then display the home screen
* If the session ID does not exist then redirect the user to the login page.
*/
String sessionId = Session.getSessionId(getApplicationContext());
if (StringUtils.isBlank(sessionId)){
navigateToLoginActivity();
}
// Display the Home Screen
setContentView(R.layout.home);
TextView tv = (TextView)findViewById(R.id.welcome);
tv.setText(sessionId);
}
public void logout(View view){
navigateToLogoutActivity();
}
public void navigateToLoginActivity(){
Intent loginIntent = new Intent(getApplicationContext(),LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(loginIntent);
}
public void navigateToLogoutActivity(){
Intent logoutIntent = new Intent(getApplicationContext(),LogoutActivity.class);
logoutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(logoutIntent);
}
}
LoginActivity
public class LoginActivity extends Activity {
EditText nfcET;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
nfcET = (EditText)findViewById(R.id.nfckey);
}
public void login(View view){
String nfckey = nfcET.getText().toString();
RequestParams params = new RequestParams();
params.put("nfckey", nfckey);
invokeWS(params);
}
public void invokeWS(RequestParams params){
// REMOVED WEBSERVICE CODE - calls webservice and retrieves a sessionID which is saved in a SharedPreference, it calls navigateToHomeActivity upon successful login
}
public void navigatetoHomeActivity(){
Intent homeIntent = new Intent(getApplicationContext(),HomeActivity.class);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homeIntent);
}
}
LogoutActivity
public class LogoutActivity extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Session.removeSessionId(getApplicationContext());
if (!StringUtils.isBlank(Session.getSessionId(getApplicationContext()))){
//TODO - Handle the case that the session was not removed!
} else {
setContentView(R.layout.logout);
}
}
}
答案 0 :(得分:0)
这是你遇到困难的部分:
if (StringUtils.isBlank(sessionId)){
navigateToLoginActivity();
}
每当您需要通过单击后退按钮退出应用程序时,它就是最后一次访问的主页。并且因为主页将始终检查您是否已登录,并且在后者的情况下将您重定向到登录页面,您真的被困住并且是实时的;)
覆盖按键事件:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
// Here you can put your boolean variable
isOutOfApp = true;
return true;
}
return super.onKeyDown(keyCode, event);
}
这里可以做到:
if (StringUtils.isBlank(sessionId)){
if(isOutOfApp) {
// show a confirmation message
// and out of the app
} else {
navigateToLoginActivity();
}
}
答案 1 :(得分:0)
要离开循环,请让LoginActivity登录并使用startActivtyForResult返回是否成功:
在HomeActivity中:
将navigateToLoginActivity更改为如下所示:
public void navigateToLoginActivity(){
Intent loginIntent = new Intent(getApplicationContext(),LoginActivity.class);
startActivityForResult(loginIntent, REQ_LOG_IN);
}
在LoginActivity中:
将此添加到您的onCreate:
setResult(RESULT_CANCELED);
将您的navigationtoHomeActivity更改为:
public void navigatetoHomeActivity(){
setResult(RESULT_OK);
finish();
}
返回HomeActviity:
将此块移动到onResume()(因为在onResume之前调用onActivityResult)
/*
* Check if a session ID exists - if it does then display the home screen
* If the session ID does not exist then redirect the user to the login page.
*/
String sessionId = Session.getSessionId(getApplicationContext());
if (StringUtils.isBlank(sessionId)){
navigateToLoginActivity();
}
添加此方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == REQ_LOGIN && resultCode == RESULT_CANCELED){
finish();
}
}