0

Can anybody explain to me one thing? I'm trying to use OnPause() for my application. When a user is choosing for the other app and my app no longer visible  OnPause() of my app starts perfectly. I'm trying to use in OnPause() some method, for example, to save some data. I noticed that in OnPause() I can operate with some public variables of my app (to assign data, to retrieve data and so on), but I have a problem with my public string array filled out in OnCreate.
If I'm trying, for example, to do: String temp_str = my_array[1] in OnPause(), my app crashes with an explanation like "Source code does not match the bytecode". Can anyone make a hint about why this happens: public variables are being used fine in OnPause(), but with the public arrays I have a problem?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
newman
  • 149
  • 10
  • You may find a solution on [this other post](https://stackoverflow.com/questions/39990752/source-code-does-not-match-the-bytecode-when-debugging-on-a-device) – Néstor Carreño Gimeno Nov 13 '19 at 08:59
  • thank you, but in my case the post doesn't help. also I made the max possible SDK level but the problem still presents – newman Nov 13 '19 at 10:29

1 Answers1

1

From the Android Developing Guide, in the onPause() lifecycle section found here:

onPause() execution is very brief, and does not necessarily afford enough time to perform save operations. For this reason, you should not use onPause() to save application or user data, make network calls, or execute database transactions; such work may not complete before the method completes. Instead, you should perform heavy-load shutdown operations during onStop()

So, the best solution is to move your method calls to the onStop() application status. onStop() gets called after onPause(), when the application is no longer visible or whenever you are about to completely close your app.

  • in my case OnStop() also does not help. The app crushes when String temp_str=my_array[1] and im pretty shure that the array is fine and exist and filled out. I somebody has time it will be interesting to make such a simple case: to fill out an array in OnCreate ant to address then to one in OnStop. Will it cause a crush? – newman Nov 13 '19 at 10:51
  • You can access it, as long as `onDestroy()` has not been called – Néstor Carreño Gimeno Nov 13 '19 at 11:03
  • 1
    Yes, I believe it shall work, but in my case, this obviously simple code crashes. Therefore I will be appreciating if someone tries it on his own computer. – newman Nov 13 '19 at 11:13