在Android的活动同步方法方法、Android

2023-09-12 22:23:42 作者:陌路人

我有两个活动。在活动1我写一个值SQLite数据库通过输入值转换成一个EditText场。有一次,我preSS提交按钮,数据被写入和我一起到另一个活动转移 - 活性2.活性2也做同样的事情,我可以写入新的数据并将其保存到一个按钮,单击该数据库。所以,我的问题是:我需要,才能在数据库中同步活动2的方法(这是一个单独的线程从UI)的活动1,以及该方法prevent线程安全问题?请记住,这两种方法访问同一个数据库,但是从不同的活动。从技术上讲,活动1应完成的onStop已被调用,新的活动(活动2),现在是可见的。它甚至有可能这两个活动及其数据库的访问(通过不同的方法),相应的线程都给予了Android活动的生命周期的任何种类的并发性问题?

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?

非常感谢, 马特

推荐答案

勾选此方法

SQLiteDatabase.setLockingEnabled(boolean lockingEnabled)