【问题标题】:Resetting activity when data cleared数据清除时重置活动
【发布时间】:2018-03-01 13:21:10
【问题描述】:

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

@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();

不幸的是,我无法让它工作。谁能指出手头的问题?

【问题讨论】:

  • 如果是全新安装,怎么会有数据要填呢?如果是重新安装,那么if (currentVersionCode == savedVersionCode) { 将运行
  • @LanceToth 这也是我想知道的。当我在手机上从 Play 商店安装该应用程序时,它隐藏了 EditTextImageButton。如果我想防止这种情况,我需要输入什么:} else if (savedVersionCode == DOESNT_EXIST) { //TODO This is a new install?我以为resetAll(); 会处理好它。
  • SharedPreferences 在安装之间保持不变,这可能是问题吗?
  • 没错,当我推送更新时它很有用,因此用户的数据保持不变。我认为当用户卸载应用程序时它不会持续存在?

标签: java android sharedpreferences reset


【解决方案1】:

SharedPreferences 不会在卸载时清除(至少自 Marshmallow 以来没有)。这可能会帮助您解决它SharedPreferences are not being cleared when I uninstall

【讨论】:

  • 啊,我明白了。所以这意味着checkFirstRun() 没用,对吗?
  • 如果您想检查版本更改,则不要
  • 是的,你是对的。我在Manifest.xml 中更改了android:allowBackup="false",我认为这可以解决问题。这让我觉得我的savedData() 有一个错误,因为它会自动隐藏EditTexts,即使是刚安装应用程序时也是如此。我需要在方法中制作某种if-statement 吗?感谢您的快速回答和帮助!
  • 请问您在savedData() 部分中究竟是如何做到这一点的?到目前为止,我所有的尝试都没有达到预期的结果。当然,如果不是太麻烦的话。
  • 感谢您的帮助!我已经移动了我的问题的最后一部分。你帮我解决了原来的问题。我欠你的!
猜你喜欢
  • 2021-08-24
  • 2013-08-16
  • 2022-07-21
  • 2016-10-14
  • 1970-01-01
  • 1970-01-01
  • 2015-05-29
  • 1970-01-01
相关资源
最近更新 更多