I am trying to load the saved string in a separate class in the same way.

public class Load extends AppCompatActivity{ public Load(){ } public String crip(){ sPref = getSharedPreferences("key", MODE_PRIVATE); ld = sPref.getString(KEY,""); return ld; } } 

trying to get a string from another class in this way

 Load losd = new Load(); 

load.crip () this is what I am trying to output to the logs, but when I launch the android studio it refers to the zero object here

 sPref = getSheredPreferences("key", MODE_PRIVATE); 
  • one
    Everything is bad. Activation is a monster object that when created creates a lot of things. And you redefined the constructor, literally broke the creation of the object, and then you try to pull out of it what is not in it and cannot be - iamthevoid
  • four
    If you want to get the values ​​in a separate class, you can do the following. Create a singleton, assign it to AppContext'om in onCreate in the application class, create a function in it that, turning to the app context, extracts prefs and a term from it and uses it. - iamthevoid
  • You can do the same and initit the context of activations, but this is a bad pattern, because you have to do it carefully, otherwise memorials will come up. At least the link to the context of the activit should not be stored anywhere, then it will be ok. - iamthevoid
  • activit - a component deeply integrated into the system with a life cycle managed by the system and the same system and created. You can't just create a regular user object and think that it will be a “live” activator. This is about how to try to start and go on an empty body from the car. This whole scheme is not fully working and will not work - pavlofff
  • for access to preferences, you don’t need to activate it at all, but only Context. you can solve this as suggested in the second comment or simply transfer the “live” context to your class, if possible. then use pref = context.getSheredPreferences("key", MODE_PRIVATE); - pavlofff

0