Android: 按两次后退恢复会话
Android: Session is restored when back is pressed twice
我正在使用 android 制作一个登录应用程序。一切正常,但问题是当按下后退按钮两次时,注销的会话将恢复吗?好像是什么问题
用于注销的代码片段
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
调用注销方法
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Launching All products Activity
session.logoutUser();
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
});
登录码:
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
//System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
//tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
String username = et.getText().toString().trim();
if(response.equalsIgnoreCase("User Found")){
session.createLoginSession(username);
runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(Login.this,"Login Success", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Login.this, AndroidTabLayoutActivity.class));
}
});
}else if(response.equalsIgnoreCase("Not Employee")){
showAlertNotEmployee();
}else if(response.equalsIgnoreCase("Disabled")){
showAlertDisabled();
}else{
showAlertNotFound();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlertNotFound(){
Login.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setTitle("Login Error.");
builder.setMessage("User not Found.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
public void showAlertDisabled(){
Login.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setTitle("Login Error.");
builder.setMessage("Account Disabled.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
public void showAlertNotEmployee(){
Login.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setTitle("Login Error.");
builder.setMessage("Employee Account Only")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
尝试添加到 Activity 标签中的 androidmanifest.xml
android:noHistory="true"
我正在使用 android 制作一个登录应用程序。一切正常,但问题是当按下后退按钮两次时,注销的会话将恢复吗?好像是什么问题
用于注销的代码片段
public void logoutUser(){
// Clearing all data from Shared Preferences
editor.clear();
editor.commit();
Intent i = new Intent(_context, Login.class);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
调用注销方法
btnLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
// Launching All products Activity
session.logoutUser();
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
}
});
登录码:
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
//System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
//tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
String username = et.getText().toString().trim();
if(response.equalsIgnoreCase("User Found")){
session.createLoginSession(username);
runOnUiThread(new Runnable() {
public void run() {
//Toast.makeText(Login.this,"Login Success", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Login.this, AndroidTabLayoutActivity.class));
}
});
}else if(response.equalsIgnoreCase("Not Employee")){
showAlertNotEmployee();
}else if(response.equalsIgnoreCase("Disabled")){
showAlertDisabled();
}else{
showAlertNotFound();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlertNotFound(){
Login.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setTitle("Login Error.");
builder.setMessage("User not Found.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
public void showAlertDisabled(){
Login.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setTitle("Login Error.");
builder.setMessage("Account Disabled.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
public void showAlertNotEmployee(){
Login.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(Login.this);
builder.setTitle("Login Error.");
builder.setMessage("Employee Account Only")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
}
尝试添加到 Activity 标签中的 androidmanifest.xml
android:noHistory="true"