我正在使用android制作登录应用程序。 Evrything工作正常,但问题是当按下后退按钮两次会话注销将被恢复?什么似乎是问题
用于退出的代码段
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();
}
});
}
}
答案 0 :(得分:0)
尝试添加到Activity标记中的androidmanifest.xml
android:noHistory="true"