Newbie question.
There is a counter, and the data is saved using SharedPreferences.
Need to hang a button to reset the data. How can this be done?
Newbie question.
There is a counter, and the data is saved using SharedPreferences.
Need to hang a button to reset the data. How can this be done?
Delete the SharedPreferences file or write the value of the counter 0. It is clear that when a file is deleted, all other parameters stored in it will be reset.
ResetCounterActivity.java public class ResetCounterActivity extends Activity { public static final String SHARED_PREFS_NAME = "counter"; // Имя файла, где хранится счетчик @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reset_counter); } public void onResetCounterClicked(View view) { getSharedPreferences(SHARED_PREFS_NAME,0).edit().clear().commit(); } } activity_reset_counter.xml <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Reset counter" android:onClick="onResetCounterClicked" android:layout_gravity="center" /> </FrameLayout> Source: https://ru.stackoverflow.com/questions/410260/
All Articles