I have an application on android in which there are such activites: login screen (LoginActivity), main screen (MainScreen). The logic is that the user enters a password and username when logging in and if everything is ok, if the server has sent its approval, then we go to the main screen. On the login screen there is a toolbar on which the button for selecting the application language is hanging. The whole principle of changing the language I described in my previous question: A complete change of language in the android application . That is, when I click on a list item, my LoginActivity re- LoginActivity is automatically done and the language is saved, for subsequent activations and for the set after the application is restarted.

And now what exactly is my problem: if you fill in the fields with login and username and then do not click on the button to send a request to the server. Instead of sending a request to the server, we decide that we first need to change the language in the application, and accordingly click on the button on the toolbar -> then select the language from the list and click on it. In this case, the fields hang filled but the "Login" button was not pressed. And then some kind of magic happens)) After choosing a language, I have made recreate activations, but for some reason I switch to the main screen, although I have a clear condition that I switch to the main screen only under two conditions - if the user already previously used the application, or the user pressed a button and a satisfactory answer came from the server.

Here are two mentions of the main screen to which the transition is made: 1)

 if (response.isSuccessful()) { Intent intent = new Intent(LoginActivity.this, MainScreen.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); finish(); overridePendingTransition(0, 0); 

2)

 sp = getSharedPreferences(REFRESH_TOKEN, MODE_PRIVATE); final boolean hasVisited = sp.getBoolean("hasVisited", false); user.setChecked(hasVisited); if (hasVisited) { Intent intent = new Intent(LoginActivity.this, MainScreen.class); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); startActivity(intent); finish(); overridePendingTransition(0, 0); 

I can not understand what could be the problem, I really hope for your help.

  • In what cases is hasVisited recorded in hasVisited ? - Enikeyschik September
  • if the user has logged in at least once, false is set if the user has clicked logout - Andrew Goroshko
  • And this is what it does: user.setChecked(hasVisited); ? - Enikeyschik September
  • This checkbox is set, now I can add an answer, if this information helps you - Andrew Goroshko
  • It appears that hasVisited reads true . See the value of the variable after reading in progress. - Enikeyschik September

1 answer 1

Oddly enough, but I went to find a solution to my problem. So let's start first: the problem was this - if we enter the password and login in the fields on the login screen, and then click the "remember user" checkbox, after we select the language and re-create the activation, our application automatically switches to the second activation as we are hanging in the settings that the user has already been here)) What I came up with (maybe crooked, but it works), when choosing a new language, we need to re-create the activation, I threw such a piece of code:

 private void changeLang(String lang) { if (lang.equalsIgnoreCase("")) return; Locale myLocale = new Locale(lang); Locale.setDefault(myLocale); android.content.res.Configuration config = new android.content.res.Configuration(); config.locale = myLocale; getBaseContext().getResources().updateConfiguration(config, getBaseContext().getResources().getDisplayMetrics()); saveLocale(lang); invalidateOptionsMenu(); sp = getSharedPreferences(REFRESH_TOKEN, MODE_PRIVATE); final boolean hasVisited = sp.getBoolean("hasVisited", false); if (hasVisited) { SharedPreferences.Editor editor = sp.edit(); editor.putBoolean("hasVisited", false); editor.apply(); changeLang(lang); } else { moveTo(); } } private void moveTo() { Intent intent = new Intent(this, LoginActivity.class); startActivity(intent); finish(); overridePendingTransition(0, 0); } 

As you can see from the code, before activating the re-creation, we check if the checkbox was activated by pulling the value of the variable from the settings. If this value is true, that is, the checkbox has been selected, then we automatically transfer it to the unselected state, check again, and then re-create the activation.

I do not argue that the method is very cumbersome, and it may be possible to solve my problem in some other way, but at this stage this method works and I personally invented it. So I decided to share the results of my work, and maybe someone will suggest improving my method in some way.

PS The only thing that I can not understand is why a variable can normally be pulled out of me only if there is a line:

 sp = getSharedPreferences(REFRESH_TOKEN, MODE_PRIVATE); 

rather, it is not that what this line does, but that there is a variable REFRESH_TOKEN . I can not understand what it affects so much, but if you replace it, then nothing works.

  • The first parameter in getSharedPreferences is the name of the settings that are being read. Somewhere in the code of the variable REFRESH_TOKEN , a string value is assigned and the settings are saved under this name ( reference ). - Enikeyschik September
  • @ Enikeyschik, you are absolutely right, this is also a setting, but for some reason when I try to read the settings with the name "hasVisited" what I did does not work for me. - Andrew Goroshko
  • These are different things. In this case: REFRESH_TOKEN - the name of the file where the settings are stored (there are a lot of settings, and there may be a lot of files too). hasVisited is the name of one single settings parameter, which is stored in a file with the name specified in REFRESH_TOKEN. - Enikeyschik
  • well then a logical question, maybe you don’t need to create a bunch of settings files, can you just create one file where and write all the settings ?? Where can I see the docks that are created during the program? - Andrew Goroshko
  • Yes, of course not. Is that in the application 100,500 settings and I want to separate them logically. And they are stored somewhere in the application folder, something like /data/data/YOUR_PACKAGE_NAME/shared_prefs/YOUR_PREFS_NAME.xml or similar ( question on SO ) - Enikeyschik