Logout function for Login Activity in Android
|
This sample is in JAVA for Android Platform: I found the following Login Activity (-> http://androidsamples.blogspot.com/2009/06/how-to-use-http-connection-saxparser.html ) samlpe which proved to be extremely usefull for my android app. The only issue that wasnt resolved within the program is the logout functionality. Below is my version of the logout. Change welcome.java to add a logout button, please keep in mind that xml layout would need to be changed as well:
inside onCreate, i've addded a button that deletes username and password from the Preferences
buttonLogout = (Button) findViewById(R.id.btn_logout);
buttonLogout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
SharedPreferences.Editor editor=mPreferences.edit();
editor.remove("UserName");
editor.remove("PassWord");
editor.commit();
Message myMessage=new Message();
myMessage.obj="NOTSUCCESS";
handler.sendMessage(myMessage);
finish();
}
});
New Handler function is added to redirect to another activity
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String loginmsg=(String)msg.obj;
if(loginmsg.equals("NOTSUCCESS")) {
Intent intent = new Intent(getApplicationContext(), LoginError.class);
intent.putExtra("LoginMessage", "Logged Out");
startActivity(intent);
removeDialog(0);
}
}
};
THAT IS IT. LET ME KNOW IF YOU HAVE ANY QUESTIONS.
|