This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have two activities. In activity 1 I write a value to a SQLite database by entering that value into an EditText field. Once I press the submit button, the data is written and I am moved along to another activity - activity 2. Activity2 also does the same thing in that I can write new data and persist it to the database with a button click. So, my question is: do I need to synchronize the method (which is on a separate thread from the UI) in activity 1, as well as the method in activity 2 in order to prevent thread safety issues in the database? Keep in mind that both methods access the same database but from different activities. Technically, activity 1 should be complete with onStop having been called and the new activity (activity 2), now being visible. Is it even possible for these two activities and their corresponding threads accessing the database (via different methods) to have any kind of concurrency problems given the Android activity life cycle?
Since both the activities 1 & 2 would be accessed by a button click from the respective UI of the activity, it seems impossible to have a concurrent database access issue in case of these two activities. A user cannot click two buttons in two different UIs at the same time.
Does this answer your query?
Joined: Aug 14, 2010
This was my thinking as well. My only concern was the slim possibility of the database hanging for some reason, since the thread could technically continue to run from activity 1 even after that activity has stopped. However, the chances of that happening are, I think, pretty remote. I just wanted to get a second opinion.