Activity life cycle onStop: Can I save all my databse table updates in onStop() of activity? If not explain why and also explain where should I save db tables?
Yes we can.
No, save it in onSaveInstanceState
No, because onStop will never be called
No, because onStop() may not be called in some situations.
In case of low memory or configuration changes, there is a chance that android may force-kill your application before reaching onStop(). onPause() is the only function that will be called with out fail before killing the application. so save all persistent data like DB tables in onPause() only.
Note : We can't save all database tables in onSaveInstanceState, because that function will not be called if user presses back button.
according to me if you are killing your application lifecycle methods called will be on on pause>onstop>ondestroy.
Although the onPause() method is called before onStop(), you should use onStop() to perform larger, more CPU intensive shut-down operations, such as writing information to a database.
Source: android developer official site
If System decides to close the app due ANR or low memory, then only onPause() and next is onDestroy()
I agree with Riaz but still i have one doubt that onStop is not guaranteed to be called when app is killed. so when should the database be updated?
onPause() surely is not the right place to update the database as it will make the app jittery.
when at point of time you call finish() method then onstop() and onDestroy() will never get called.. so onPause() is the only option left.
Back To Top