0

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?

Jamie Coenen
  • 75
  • 1
  • 10
  • If it is a new install, how can there be data to fill? If it's a reinstall, then `if (currentVersionCode == savedVersionCode) {` will run – Lance Toth Mar 01 '18 at 13:26
  • @LanceToth That is what I was wondering too. When I installed the app from the Play Store on my phone, it hid the `EditText` & `ImageButton`. What do I need to input on: `} else if (savedVersionCode == DOESNT_EXIST) { //TODO This is a new install` if I want to prevent this? I thought `resetAll();` would take care of it. – Jamie Coenen Mar 01 '18 at 13:56
  • SharedPreferences is persistent between installs, might that be the problem? – Lance Toth Mar 01 '18 at 14:03
  • True, it is useful when I push updates, so the user's data persists. I thought it didn't persist when a user uninstalls the app? – Jamie Coenen Mar 01 '18 at 14:04

1 Answers1

0

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

Lance Toth
  • 430
  • 3
  • 17
  • Ah, I see. So this means `checkFirstRun()` is useless, am I right? – Jamie Coenen Mar 01 '18 at 14:11
  • Not if you want to check for version changes – Lance Toth Mar 01 '18 at 14:12
  • Yes, you are right. I changed `android:allowBackup="false"` in my `Manifest.xml`, which I think solves the problem. Which makes me think there is a mistake in my `savedData()`, because it automatically hides the `EditTexts`, even when freshly installing the app. Do I need to make some sort of `if-statement` in the method? I am grateful for your quick answers and help! – Jamie Coenen Mar 01 '18 at 14:20
  • May I ask you how exactly to do that in the `savedData()` part? All my attempts thus far, did not have the expected result. If it is not too much of a hassle of course. – Jamie Coenen Mar 01 '18 at 14:29
  • Simply check if any of the data is there (that's guaranteed to be there) – Lance Toth Mar 01 '18 at 14:38
  • Would `if (goalSave != "Beams") { goalSave = preferences.getString("goals", goalSave); }` work? Putting it above the `preferences` initiator, but below the `String goalSave = showGoalTextView.getText().toString();`? – Jamie Coenen Mar 01 '18 at 14:43
  • `String goalSave = showGoalTextView.getText().toString();` gets the contents of the field, which I assue is empty anyway. Simply put `if (goalSave != null) {` after `goalSave = preferences.getString("goals");` – Lance Toth Mar 01 '18 at 14:53
  • Now I have: `goalSave = preferences.getString("goals", goalSave); if (goalSave != null) { showGoalTextView.setText(goalSave); } else { }` I assume I need to do the same for beamsSave, only then do `if (beamSave != "0") { showBeamsTextView.setText(beamsSave)`, because the default set value of the `TextView` is 0, but it is converted to string so I can't use an `int`. – Jamie Coenen Mar 01 '18 at 15:02
  • 1
    Thank you for helping me out! I have moved the last piece of my question. You helped me out with the problem as was originally. I owe you! – Jamie Coenen Mar 01 '18 at 17:09