'Android logout method and clear sharedpreference is not working
I have a main activity that launches a LoginActivity for the first time. Here everythig goes ok. When the user logs in, the user's information are saved in sharedpreferences (to make request to ApiRest) so this function that launch login only the first time is working ok because the next time I start the app I stay in mainActivity.
This main activity has 3 fragments (in viewpager) the 3 fragments when are created make request to get information from the api automatically.
My problem is in the appbar of the mainActivity I have a menu item that logout the user. My intention is to redirect the app to loginActivity and clear the stack of activities and clear the sharedpreferences.
But I get redirected to the login activity but when I close the app and open it again it does not respect my method in main activity (that if it is the first time it must redirect to LoginActivity) and creates the 3 fragments that inmediately start to make the requests but they have not the parameters (they are null or 0 since I clear the shared preferences) so the app crashes.
Why is my method to open first time LoginActivity omited?
Here is my MainActivity with the method that is failing or omitting after logout:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Variables variableGlobal = (Variables)getApplicationContext();
SharedPreferences sharedPreferences= getSharedPreferences("firstTime",this.MODE_PRIVATE);
boolean firstTime = sharedPreferences.getBoolean("firstTime",true);
if(firstTime){
SharedPreferences.Editor edit = sharedPreferences.edit();
edit.putBoolean("firstTime", false);
edit.commit();
startActivity(new Intent(MainActivity.this, LoginActivity.class));
finish();
}....
here is my logout code:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.logoutmain) {
//Here i clear the sharedpref datos wich contains the user information
SharedPreferences pref = getSharedPreferences("datos",MODE_PRIVATE);
SharedPreferences.Editor editorpref = pref.edit();
editorpref.clear();
editorpref.commit();
//Here i clear the sharedPref that contains the value (boolean) to say the app if it is the first time or not
SharedPreferences pref2 = getSharedPreferences("firstTime",this.MODE_PRIVATE);
SharedPreferences.Editor editorpref2 = pref2.edit();
editorpref2.putBoolean("firstTime",true);
editorpref2.commit();
System.out.println("shared pref cleared");
Intent intent=new Intent(getApplicationContext(),MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return true;
}...
here is my loginActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
btnLogin= (Button)findViewById(R.id.btnLogin);
api= ApiUtils.getAPIService();
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
login();
}
});
}
When I run my app, the method to check if it is the firstTime that is running it launch to LoginActivity is working ok, it is launched only the firsTime and the next time I always open in Main Activity, but when I logout it redirects me to Login and clears the stack and the sharedpref, but omits the method to check that is the first time and keeps me in MainActivity and the app crashes. Why?
Solution 1:[1]
I see a problem in your logic, the quickFix would be to use the code below in your MainActivity:
boolean notFirstTime = sharedPreferences.getBoolean("firstTime" , false);
if (! notFirstTime){
SharedPreferences.Editor edit = ...
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nabin Bhandari |
