1

I'm studying Android Activity lifecycle and I'm pretty confused about some topics. I found, even on this forum, documents/books that state the opposite of other document/books.

For example, the book I bought states:"onPause()... Steps should be taken within this method to store any persistent data required by the activity (such as data stored to a content provider, database or file)...

On the other hand, android official guidelines state "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()"

But, we know that onStop() is not sure to be called everytime!!!

Who's right??

Thank you in advance.

MDP
  • 4,177
  • 21
  • 63
  • 119
  • the things that the official documentation says you shouldn't be doing in onPause, you shouldn't be doing them on the UI thread anyway, you should keep execution time of all lifecycle methods fast – lelloman Mar 09 '17 at 13:47
  • You got pretty much the same question [here](http://stackoverflow.com/questions/29480890/when-to-save-data-to-database-onpause-or-onstop) :) – w00ly Mar 09 '17 at 14:00
  • Possible duplicate of [When to save data to database, onPause() or onStop()?](http://stackoverflow.com/questions/29480890/when-to-save-data-to-database-onpause-or-onstop) – w00ly Mar 09 '17 at 14:00
  • Thank you for the links, but I still have my doubts. In those links there are contradictory statements as in the document/books I read. One guy says a thing and another one says the opposite! – MDP Mar 09 '17 at 14:14

1 Answers1

1

Just do your savings inside a method which controls a flag. eg.

public void method(){
  if (!done){
     [...] savings
  }
  done = true;
}

And call it in onPause() and in onStop(). So, if method reach the end, done is true, so the second call won't be executed, otherwise, onStop() will perform what you need.

That is MY "solution", my opinion, it might be wrong :)

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
  • Thank you for the answer, this is what I already do, but according to google documentation, we should not do it!! I was looking for an official google answer, but seems that every single developer has to find his own best solution! :D Saluti da Roma – MDP Mar 09 '17 at 14:19
  • Yeah, it's more a personal decision, saluti da Milano :D – Luca Nicoletti Mar 09 '17 at 14:26