清除数据时重置活动

时间:2018-03-01 13:21:11

标签: java android sharedpreferences reset

我目前正在开发一款应用,但它有一个错误。每当用户安装应用程序或清除数据时,都应该重置。但相反,应用程序会为用户填写标准数据,而不是显示用户可以自己输入的开始屏幕。

@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");
}

我的问题主要是checkFirstRun方法。 我想输入:

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

不幸的是,我无法让它发挥作用。任何人都可以指出手头的问题吗?

1 个答案:

答案 0 :(得分:0)

卸载时不会清除SharedPreferences(至少从Marshmallow开始)。这可能有助于您解决问题SharedPreferences are not being cleared when I uninstall