Sunday, August 4, 2019

sharedpreferences - How to delete shared preferences data from App in Android

None of the answers work for me since I have many shared preferences keys.



Let's say you are running an Android Test instead of a unit test.



It is working for me loop and delete through all the shared_prefs files.




@BeforeClass will run before all the tests and ActivityTestRule





@BeforeClass
public static void setUp() {
Context context = InstrumentationRegistry.getTargetContext();

File root = context.getFilesDir().getParentFile();
String[] sharedPreferencesFileNames = new File(root, "shared_prefs").list();
for (String fileName : sharedPreferencesFileNames) {
context.getSharedPreferences(fileName.replace(".xml", ""), Context.MODE_PRIVATE).edit().clear().commit();
}

}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...