my dog learned polymorphism*
The moose likes Android and the fly likes synchronized methods in Android Activities Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Mobile » Android
Bookmark "synchronized methods in Android Activities" Watch "synchronized methods in Android Activities" New topic
Author

synchronized methods in Android Activities

Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
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?

Thanks very much, Matt
Harvinder Thakur
Ranch Hand

Joined: Jun 10, 2008
Posts: 231
Hi,

I hope i've understood your question correctly!!

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?


thanks
Harvinder
Matt Pavlovich
Ranch Hand

Joined: Aug 14, 2010
Posts: 98
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.

Thanks for your input.
Matt
 
Don't get me started about those stupid light bulbs.
 
subject: synchronized methods in Android Activities