I am working on the android application that required login. I want to check when user press the PostIdea activity button then check the user logged in or not, if not then redirect to the login page else continue with the task.
Here I am using SharedPreferences for that. Now when I press the login button then username & password add to the SharedPreferences and when press the PostIdea Activity then I got the username & password.
BUT my problem is that when I restart the application and go the PostIdea Activity without login then it remember the last login(username/password) of SharedPreferences data.How can I remove that data from SharedPreferences,so that my application works correctly. Let me know where I do mistake or there is any other solution for my problem.
Thanks.
LoginActivity.class
public class LoginActivity extends CheerfoolznativeActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
setHeader("Login");
editUser = (EditText) findViewById(R.id.login_useredit);
editPass = (EditText) findViewById(R.id.login_passedit);
txterror = (TextView) findViewById(R.id.login_error);
textlogin = (TextView) findViewById(R.id.login_postidea_textView);
btngotoregister = (Button) findViewById(R.id.login_btnLinkToRegisterScreen);
btnlogin = (Button) findViewById(R.id.login_button);
pgb = (ProgressBar) findViewById(R.id.login_progressBar);
btnlogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Login().execute();
}
});
}
public class Login extends AsyncTask<Void, Void, Void> {
int i = 0;
String uName ;
String Password;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
uName = editUser.getText().toString().trim();
Password = editPass.getText().toString().trim();
if (uName.equals("") | Password.equals("")) {
Toast.makeText(getApplicationContext(), "Enter the Data",
Toast.LENGTH_SHORT).show();
if (editUser.length() == 0) {
editUser.setError("Enter Username");
}
if (editPass.length() == 0) {
editPass.setError("Enter Password");
}
} else {
pgb.setVisibility(View.VISIBLE);
}
}
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
String loginURL = "http://www.cheerfoolz.com/rest/user/login";
strResponse = util.makeWebCall(loginURL, uName, Password);
return null;
}
@Override
public void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
txterror.setText("");
try {
if (strResponse.substring(KEY_SUCCESS) != null) {
txterror.setText("");
// SharedPreferences Logic
SharedPreferences userDetails =getSharedPreferences("userdetails", MODE_PRIVATE);
Editor edit = userDetails.edit();
edit.clear();
edit.putString("username", uName);
edit.putString("password", Password);
edit.commit();
new FetchUserProfileTask().execute();
} else {
txterror.setText("Username and Password Not valid !!!");
}
} catch (Exception e) {
// TODO: handle exception
}
}
}
public class FetchUserProfileTask extends AsyncTask<Void, Void, Void> {
int i = 0;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... params) {
//logic for the featch user profile data
return null;
}
@Override
public void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pgb.setVisibility(View.GONE);
displayProfile();
}
}
public void displayProfile() {
Intent in1 = new Intent(LoginActivity.this, post_myprofile.class);
in1.putExtra("loginObject", bean);
startActivity(in1);
}
}
Post_idea_Activity.class
public class Post_idea_Activity extends CheerfoolznativeActivity {
TextView txtwelcome;
String Uname="";
String pass = "";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences userDetails = getSharedPreferences("userdetails", MODE_PRIVATE);
System.out.println("value of the userdertails ==========>"+ userDetails);
//String Uname="";
//String pass= "";
Uname = userDetails.getString("username", "");
pass = userDetails.getString("password", "");
Toast.makeText(getApplicationContext(),"username : "+Uname +" \n password :"+pass, Toast.LENGTH_SHORT).show();
if (Uname.equals(null))
{
Intent in1 = new Intent(this, LoginActivity.class);
in1.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(in1);
}
else {
setContentView(R.layout.post_idea);
setHeader("Post idea");
txtwelcome =(TextView)findViewById(R.id.post_welcome_text);
}
}
// coding for the layout
}