Hello! I have several Activities, one of them is to activate it with the settings of the application that I create. In the settings, I created a button, when clicked, all of the application settings should be reset, including all shared preferences, which are contained in different Activities. This method is implemented

public class Settings extends MainActivity {

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_settings); View view = findViewById(R.id.buttonReset); if (view != null) { view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { clearData(Settings.this); } }); } } public static void delete(File file, boolean deleteDir) { if (file.isDirectory()) { File[] files = file.listFiles(); if (files != null) { for (File f : files) { delete(f, true); } } if (deleteDir) { file.delete(); } } else { file.delete(); } } public static void clearData(Context context) { File files = context.getDir("tmp", Context.MODE_PRIVATE); delete(files.getParentFile(), false); } 

}

Settings are deleted, because in the settings of the smartphone in the "Application Management" tab in the settings of the application in the "Data" line is 0.00B. That is, the data is deleted. But all shared preferences for some reason did not leave. That is, the whole progress of the game (levels, coins, etc.) is preserved, because it is contained in shared preferences. Tell me what I'm doing wrong. And is there another way to solve the problem? Thank.

    1 answer 1

    Get SharedPreferences, call the edit () method of this object. You will be returned to the Editor. Call clear () on it. After that, the Editor call commit ().

    • This of course works, but only for a single preferences file. And I have a bunch of them in the application. Need to delete everything. - Denis Lolik
    • @Alex Les deleting the settings directory will not achieve anything, since the settings will remain in the application memory and at the first opportunity they will be saved to disk. SharedPreferences have to open each SharedPreferences and clean hand to hand - Barmaley
    • Is it through the intents to get to the other Activity to clean up SharedPreferences? because It is impossible to clean up all SharedPreferences from akstiviti settings. I understand correctly? - Denis Lolik
    • one
      @Alex Les, why do you have separate Preferences for each Activity? Use the PreferenceManager.getDefaultSharedPreferences (Context context) construct and store there. It will be convenient to clean them later. - Lucky_spirit
    • Hmm, I did not know about it, thanks for the advice. I will try now. - Denis Lolik