-1

Think of an activity that contains a toggle button and user can frequently change its state and I have to save that state in database . It does not seem rational to save it on every click ! so I decided to save such changes in onStop() of my activity but I'm wondering is this the correct place ? what do you suggest for that . thanks a lot

1 Answers1

1

Here is an image of the lifecycle of activities:

activity lifecycle

When you save the data in a database in the onStop() method, the data is saved when the activity is no longer visible. This is probably what you want.

onDestroy() is not the correct place to save the data to the database, because there is no guarantee that onDestroy() will be called (see this StackOverflow question

You can use onPause() as well if you want. For example, when there is a translucent activity opened, onPause() is called, but onStop() not because the activity is still visible.

Joost Verbraeken
  • 883
  • 2
  • 15
  • 30