I find this strange, but when I uninstall my app and install it again it restores SharedPrefs which is responsible for my progress bar progress. I’ve tried allow backup=false and android:fullBackupOnly=false but it doesn’t work.
Here is the code:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mBinding = ActivityMainBinding.inflate(layoutInflater)
val view = mBinding.root
setContentView(view)
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACTIVITY_RECOGNITION)
!= PackageManager.PERMISSION_GRANTED) {
showRationalDialogForPermissions()
} else {
val MY_PERMISSIONS_REQUEST_ACTIVITY_RECOGNITION = 1
ActivityCompat.requestPermissions(this,
arrayOf(Manifest.permission.ACTIVITY_RECOGNITION),
MY_PERMISSIONS_REQUEST_ACTIVITY_RECOGNITION)
}
loadData()
resetSteps()
sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
}
override fun onResume() {
super.onResume()
running = true
val stepSensor = sensorManager?.getDefaultSensor(Sensor.TYPE_STEP_COUNTER)
if (stepSensor == null) {
Toast.makeText(
this,
"No sensor for step counter detected on this device",
Toast.LENGTH_SHORT
).show()
} else {
sensorManager?.registerListener(this, stepSensor, SensorManager.SENSOR_DELAY_UI)
}
}
override fun onSensorChanged(event: SensorEvent?) {
if (running) {
totalSteps = event!!.values[0]
val currentSteps = totalSteps.toInt() - previousTotalSteps.toInt() // in resetSteps, previous total steps is equal to total steps, therefore 90 - 90 is 0
mBinding.tvStepsTaken.text = ("$currentSteps")
mBinding.progressCircular.apply {
setProgressWithAnimation(currentSteps.toFloat())
}
}
}
override fun onAccuracyChanged(p0: Sensor?, p1: Int) {
}
fun saveData() {
val sharedPrefs =
getSharedPreferences("myPrefs", Context.MODE_PRIVATE)
val editor = sharedPrefs.edit()
editor.putFloat("key1", previousTotalSteps)
editor.apply()
}
private fun loadData() {
val sharedPrefs =
getSharedPreferences("myPrefs", Context.MODE_PRIVATE)
val savedNum: Float = sharedPrefs.getFloat("key1", 0f)
Log.d("MainActivity", "$savedNum")
previousTotalSteps = savedNum
}
fun resetSteps() {
mBinding.tvStepsTaken.setOnClickListener {
Toast.makeText(this, "Long press to reset steps", Toast.LENGTH_SHORT).show()
}
mBinding.tvStepsTaken.setOnLongClickListener {
totalSteps = previousTotalSteps
mBinding.tvStepsTaken.text = 0.toString()
mBinding.progressCircular.progress = 0f
saveData()
true
}
}
So when I install app again progress bar still counts previousTotalSteps, when I Click on it it restarts to 0 but when opening app again it still has previousTotalSteps in progress bar. It’s like saving all steps in sharedPrefs regardless of calling resetSteps function or reinstalling the app itself. Even if i delete cache and data manually after reopening the app it restores data lol
2
Answers
You can also specify
android:fullBackupOnly=false
in your manifest file along withandroid:allowBackup=false
.No, I don’t think this problem is related to SharedPreferences file. It is the nature of the sensor. The Step Counter Sensor is always recording steps taken by the user, regardless of whether your app is there or not. This data is accessed through event values by the app and assigned to totalSteps variable. The only time this value resets to zero is when the phone reboots. When you reinstall your app, your shared preferences file also gets deleted and hence your previousTotalSteps value gets deleted too. But the sensor event value is not reset unless the phone reboots.
Hence after reinstallation, the code loads the step counter sensor event values and assigns to totalSteps variable, and the previousTotalSteps is zero. So,
totalSteps - previousTotalSteps
= totalSteps which is displayed in textView.Also, from your code for resetSteps(),
It should be
previousTotalSteps = totalSteps
instead of other way around. You have to essentially assign totalSteps value to previousTotalSteps variable so that totalSteps – previousTotalSteps becomes zero on resetting. Since you are resetting while assigning the values other way round, totalSteps variable again reverts to Step Counter based event value instead of what you reset it to. That’s why your totalSteps count is reappearing even after resetting.