Search code examples
javaandroidsharedpreferencesreset

Resetting activity when data cleared


I am currently working on an app, but there is a bug in it. Whenever a user installs the app or clears data, it should get reset. But instead the app fills in standard data for the user, instead of showing the start screen where user can input himself.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    checkFirstRun();
    savedData();

    }

public void savedData() {
    showGoalTextView = (TextView) findViewById(R.id.textView3);
    showBeamsTextView = (TextView) findViewById(R.id.textView2);
    showGoalEditText = (EditText) findViewById(R.id.editText2);
    checkBtnPressed = (ImageButton) findViewById(R.id.imageButton5);
    showBeamsEditText = (EditText) findViewById(R.id.editText4);
    beamsGoal = (EditText) findViewById(R.id.editText4);

    //Fetch the stored data and display it upon starting.
    String goalSave = showGoalTextView.getText().toString();
    String beamsSave = showBeamsTextView.getText().toString();

    preferences = getSharedPreferences("userData", MODE_PRIVATE);

    goalSave = preferences.getString("goals", goalSave);
    beamsSave = preferences.getString("beam", beamsSave);

    showGoalTextView.setText(goalSave);
    showBeamsTextView.setText(beamsSave);

    //Hide all the first use stuff
    showGoalEditText.setVisibility(View.GONE);
    checkBtnPressed.setVisibility(View.GONE);
    showBeamsEditText.setVisibility(View.GONE);
    beamsGoal.setVisibility(View.GONE);
}


public void checkFirstRun() {
    final String PREFS_NAME = "MyPrefsFile";
    final String PREF_VERSION_CODE_KEY = "version_code";
    final int DOESNT_EXIST = -1;

    //Get current version code
    int currentVersionCode = BuildConfig.VERSION_CODE;

    //Get saved version
    SharedPreferences preferences = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
    int savedVersionCode = preferences.getInt(PREF_VERSION_CODE_KEY, DOESNT_EXIST);

    //Check for first run or upgrade
    if (currentVersionCode == savedVersionCode) {
        //No big deal
        return;
    } else if (savedVersionCode == DOESNT_EXIST) {
        //TODO This is a new install
        resetAll();
    } else if (currentVersionCode > savedVersionCode) {
        //TODO This is an upgrade
        return;
    }

    //Update sharedPreferences with the current version code
    preferences.edit().putInt(PREF_VERSION_CODE_KEY, currentVersionCode).apply();
}

public void resetAll() {
    //For when resetting
    TextView showGoalTextView =
            (TextView) findViewById(R.id.textView3);
    EditText showGoalEditText =
            (EditText) findViewById(R.id.editText2);
    ImageButton checkBtnPressed =
            (ImageButton) findViewById(R.id.imageButton5);
    EditText showBeamsEditText =
            (EditText) findViewById(R.id.editText4);
    EditText beamsGoal =
            (EditText) findViewById(R.id.editText4);

    //Clear editTexts
    showGoalEditText.getText().clear();
    showBeamsEditText.getText().clear();

    //Show all editTexts
    showGoalEditText.setVisibility(View.VISIBLE);
    checkBtnPressed.setVisibility(View.VISIBLE);
    showBeamsEditText.setVisibility(View.VISIBLE);
    beamsGoal.setVisibility(View.VISIBLE);

    //Get the text view
    TextView showResetTextView =
            (TextView) findViewById(R.id.textView2);

    //Get the value of the text view
    String resetString = showResetTextView.getText().toString();

    //Convert value to a number and reset it
    Integer reset = Integer.parseInt(resetString);
    reset = 0;

    //Display the new value in the text view.
    showResetTextView.setText(reset.toString());
    showGoalTextView.setText("BEAMS");
}

My question is mainly on the checkFirstRun method. I want to input:

} else if (savedVersionCode == DOESNT_EXIST) { //TODO This is a new install resetAll();

Unfortunately, I can't get it to work. Can anyone point out the issue at hand?


Solution

  • SharedPreferences is not cleared with uninstall (at least, not since Marshmallow). This might help you solve it SharedPreferences are not being cleared when I uninstall